从FolderBrowserDialog获取网络路径而不是驱动器号

时间:2016-06-07 18:46:45

标签: c# winforms folderbrowserdialog

我有一个FolderBrowserDialog的winform,可以从网络驱动器中选择一个文件夹。问题是它返回驱动器号(X:\Folder...)而不是网络路径(\\Network\Projects\Folder...)。我怎样才能获得网络路径?

1 个答案:

答案 0 :(得分:2)

快速而脏的代码,它将路径显示为MessageBox中的网络路径。您可能需要添加其他检查和/或重新构建一下。

        var dialog = new OpenFileDialog();
        dialog.ShowDialog();

        var path = dialog.FileName;
        using (var managementObject = new ManagementObject())
        {
            managementObject.Path = new ManagementPath($"Win32_LogicalDisk='{path.Substring(0,2)}'");
            var driveType = (DriveType)(uint)managementObject["DriveType"];
            var networkPath = Convert.ToString(managementObject["ProviderName"]);


            if (driveType == DriveType.Network)
            {
                MessageBox.Show(path.Replace(path.Substring(0, 3), networkPath));
            }
        }

        MessageBox.Show(path);