This post will show you an easier way to map a network drive using dot net code. you are free to use any .net language of your choice. I will be mostly sticking to C# though.
We will be relying on COM Interop for this and we will be referencing the Windows Script Host Object Model for this task. Lets get going....
In Solution Explorer, right click on References and Click on Add reference.
Select the COM tab.
Scroll down and select Windows Script Host Object Model
Now the solution explorer will look like this with an IWshRuntimeLibrary added.
Now you are ready to go.
Go to the code window. select your required event [button click or form load or just anything].
Create an instance of the class IWshNetwork_Class
Call the MapNetworkDrive method and you are done.
you can use the following code to UnMap or remove the mapping.
Hope this saves some time for you....
Happy Programming...
We will be relying on COM Interop for this and we will be referencing the Windows Script Host Object Model for this task. Lets get going....
- Step 1.
- Step 2.
In Solution Explorer, right click on References and Click on Add reference.
Select the COM tab.
Scroll down and select Windows Script Host Object Model
Now the solution explorer will look like this with an IWshRuntimeLibrary added.
Now you are ready to go.
- Step 3.
Go to the code window. select your required event [button click or form load or just anything].
Create an instance of the class IWshNetwork_Class
Call the MapNetworkDrive method and you are done.
- Sample Code
IWshNetwork_Class network = new IWshNetwork_Class();
network.MapNetworkDrive("k:",@"\\192.168.20.35\MyShare", Type.Missing, "user1", "password1");
you can use the following code to UnMap or remove the mapping.
network.RemoveNetworkDrive("k:");
Hope this saves some time for you....
Happy Programming...