Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if path is on network

Tags:

c#

.net

vb.net

In my app I have a dialog in which the user can select a database backup location.
I want to warn the user if the location he/she selected is "probably not secure".

I want to consider the following locations secure:

  1. When selected folder is on a network
    (either by a mapped drive (I:\Backup) or UNC notation(\\server2\backup))
  2. When selected folder is on a different physical disk than the database folder

How can I get this kind of info about a selected folder?
I know about the DriveInfo class, but it only handles drive letters, not UNC paths.

like image 300
Robbert Dam Avatar asked Feb 11 '10 09:02

Robbert Dam


People also ask

How can I tell if a network path is accessible or not in C#?

How's this for a quick and dirty way to check - run the windows net use command and parse the output for the line with the network path of interest (e.g. \\vault2 ) and OK . Here's an example of the output: C:\>net use New connections will be remembered.

How do I find my UNC path?

In Windows, if you have mapped network drives and you don't know the UNC path for them, you can start a command prompt (Start → Run → cmd.exe) and use the net use command to list your mapped drives and their UNC paths: C:\>net use New connections will be remembered.

What is a network path?

The project management term network path refers to any consecutive or continuous series of work or project schedule activities that may form some sort of a connection with a number of logical relationships as designated in the project schedule network diagram.


1 Answers

Take a look at the PathIsNetworkPath function:

class Program
{
    [DllImport("shlwapi.dll")]
    private static extern bool PathIsNetworkPath(string pszPath);

    static void Main(string[] args)
    {
        Console.WriteLine(PathIsNetworkPath("i:\Backup"));
    }
}
like image 85
Darin Dimitrov Avatar answered Oct 10 '22 07:10

Darin Dimitrov