Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Map a Drive using C#?

Tags:

c#

.net

How can I map a network drive using C#. I don't want to use net use or any third party API.

Heard about UNC paths in C# code but not quite sure how to go about it.

like image 698
Simsons Avatar asked Aug 12 '10 06:08

Simsons


People also ask

Which CMD command allows you to map a network drive?

“Net use” is a command line method of mapping network drives to your local computer.

How do I map a network drive without the letter?

I can do this by opening my computer, right click and select "Add a network location" and then select the share. This will bring up a shared folder without a drive letter.


1 Answers

Use the WnetAddConnection functions available in the native mpr.dll.

You will have to write the P/Invoke signatures and structures to call through to the unmanaged function. You can find resources on P/Invoke on pinvoke.net.

This is the signature for WNetAddConnection2 on pinvoke.net:

[DllImport("mpr.dll")]    
public static extern int WNetAddConnection2(
   ref NETRESOURCE netResource,
   string password, 
   string username, 
   int flags);
like image 96
fletcher Avatar answered Nov 15 '22 12:11

fletcher