Using C#, I would like my application to return whether a folder (with an already known path) is located in a network or in my computer.
How can I do that?
Using C#, I would like my application to return whether a folder (with an already known path) is located in a network or in my computer.
How can I do that?
On
Original Answer from another SO question, Check if path is on network.
Use PathIsNetworkPath (pinvoke reference):
class Program
{
[DllImport("shlwapi.dll")]
private static extern bool PathIsNetworkPath(string pszPath);
static void Main(string[] args)
{
Console.WriteLine(PathIsNetworkPath("i:\Backup"));
}
}
On
Try the following from Shell Lightweight Utility API:
class Class
{
[DllImport("shlwapi.dll")]
private static extern bool PathIsNetworkPath(string Path);
[STAThread]
static void Main(string[] args)
{
string strPath = "D:\\Temp\\tmpfile.txt";
bool blnIsLocalPath = IsLocalPath(strPath);
Console.WriteLine(blnIsLocalPath.ToString());
Console.ReadLine();
}
private static bool IsLocalPath(string Path)
{
return !PathIsNetworkPath(Path);
}
}
Things to take into consideration:
On
You can use folowing method to get UNC path for a folder. Not exactly what you are looking for but might be useful
public static string GetUniversalPath(string folderPath)
{
if (String.IsNullOrEmpty(folderPath) || folderPath.IndexOf(":") > 1)
return folderPath;
if (folderPath.StartsWith("\\"))
{
return folderPath;
}
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT RemoteName FROM win32_NetworkConnection WHERE LocalName = '" + folderPath.Substring(0, 2) + "'");
foreach (ManagementObject managementObject in searcher.Get())
{
string remoteName = managementObject["RemoteName"] as String;
if (!String.IsNullOrEmpty(remoteName))
{
remoteName += folderPath.Substring(2);
return remoteName;
}
}
return folderPath;
}
If you are talking about a mapped network drive, you can use the
DriveInfo'sDriveTypeproperty: