【问题标题】:How to get FolderBrowserDialog to show only Network如何让 FolderBrowserDialog 仅显示网络
【发布时间】:2015-10-27 09:40:14
【问题描述】:

代码如下:

    private string SelectNetworkFolder(FolderBrowserDialog oFolderBrowserDialog)
    {
        Type type = oFolderBrowserDialog.GetType();
        FieldInfo fieldInfo = type.GetField("rootFolder", BindingFlags.NonPublic | BindingFlags.Instance);
        fieldInfo.SetValue(oFolderBrowserDialog, (Environment.SpecialFolder)18);
        oFolderBrowserDialog.Description = "Choose destination for the zip file(s)";

        PackageImagesViewModel viewModel = this.DataContext as PackageImagesViewModel;
        oFolderBrowserDialog.SelectedPath = viewModel.GetMediaFolder();
        if (oFolderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            return oFolderBrowserDialog.SelectedPath.ToString();
        }
        else
        {
            return "";
        }
    }

这就是事情变得疯狂的地方 -

  1. 如果 SelectedPath 设置为以反斜杠结尾的字符串,我会得到 网络和只有那个服务器 - 选择了指定的路径。
  2. 如果 SelectedPath 设置为不以 反斜杠,我从 1) 以及我的其他 3 个工作站中得到一切 网络(应该有 700 左右)。
  3. 如果我不设置 SelectedPath,我将获得网络上的所有工作站。

任何人都知道如何列出所有工作站并将当前选择作为指定文件夹吗?其中一项要求是仅显示网络共享。

【问题讨论】:

    标签: c# wpf winforms


    【解决方案1】:

    我让产品负责人更改了需求。现在用户可以选择任何文件夹,如果该文件夹不是网络共享,则会收到错误消息。

        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            PackageImagesViewModel viewModel = this.DataContext as PackageImagesViewModel;
    
            VistaFolderBrowserDialog d = new VistaFolderBrowserDialog();
            d.Description = "Please select a network share";
            d.SelectedPath = viewModel.GetMediaFolder();
            bool? pathSelected = d.ShowDialog();
            if (pathSelected == false)
                return;
            string value = GetUncPath(d.SelectedPath);
    
            if (string.IsNullOrEmpty(value))
            {
                MessageBox.Show("The selected folder is not recognized as a network path.", "Invalid Target Directory");
                return;
            }
            viewModel.DestinationFolder = value;
        }
    
        private string GetUncPath(string path)
        {
            if (path.StartsWith("\\"))
                return path;
            try
            {
                ManagementObject mo = new ManagementObject();
                mo.Path = new ManagementPath(string.Format("Win32_LogicalDisk='{0}'", path.Substring(0, 2)));
    
                // DriveType 4 = Network Drive
                if (Convert.ToUInt32(mo["DriveType"]) == 4)
                    return Convert.ToString(mo["ProviderName"]) + path.Substring(2);
                else
                    return string.Empty;
            }
            catch { return string.Empty; }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多