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