从 FolderBrowserDialog 获取网络路径而不是驱动器号
Get network path not drive letter from FolderBrowserDialog
我有一个带有 FolderBrowserDialog
的 winform,可以从网络驱动器中选择一个文件夹。问题是它 returns 驱动器号 (X:\Folder...
) 而不是网络路径 (\Network\Projects\Folder...
)。如何获取网络路径?
将路径显示为 MessageBox 中的网络路径的快速粗略代码。您可能需要添加额外的检查 and/or 稍微重组一下。
using System.Management;
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);
我有一个带有 FolderBrowserDialog
的 winform,可以从网络驱动器中选择一个文件夹。问题是它 returns 驱动器号 (X:\Folder...
) 而不是网络路径 (\Network\Projects\Folder...
)。如何获取网络路径?
将路径显示为 MessageBox 中的网络路径的快速粗略代码。您可能需要添加额外的检查 and/or 稍微重组一下。
using System.Management;
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);