I have a winform with a FolderBrowserDialog
to choose a folder from a network drive. The issue is that it returns the drive letter (X:\Folder...
) rather than the network path (\\Network\Projects\Folder...
). How can I get the network path?
Asked
Active
Viewed 2,238 times
2

Programmer
- 459
- 5
- 20
-
1What happens if you choose a network location that's not mapped to a drive? – Adam V Jun 07 '16 at 19:00
-
1See this other question, the answer is similar to what you're looking for : (http://stackoverflow.com/questions/31818511/how-to-get-folderbrowserdialog-to-show-only-network) – Peter4499 Jun 07 '16 at 19:08
-
@AdamV I'm not sure what happens if the location is not mapped. For my purposes, assume all network locations are mapped. – Programmer Jun 07 '16 at 19:16
-
@Peter4499 This will help, thank you. – Programmer Jun 07 '16 at 19:16
1 Answers
2
Quick and dirty code that will show the path as a network path in a MessageBox. You may want to add additional checks and/or restructure this a bit.
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);

Leo Gurdian
- 2,109
- 17
- 20

Buck A. Mayzing
- 128
- 8
-
1I'm getting a System.TypeInitializationException on `var driveType = (DriveType)(uint)managementObject["DriveType"];` – B Minster Sep 18 '17 at 19:53