I'm trying to map network drive from windows service, I use batch file for executing the following command
NET USE U: \\192.168.55.6\folder password
While executing batch file either in service constructor or in onstart event, drive is not mapped?
Process process = new Process();
process.StartInfo.FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\MAP.BAT";
process.StartInfo.CreateNoWindow = false;
process.Start();
How does one map network drive from windows service?
This can be achieved using a reference to Windows Script Host Object Model.
Add a COM reference to Windows Script Host Object Model which adds an entry IWshRuntimeLibrary to the references section. Now you can use the following code to map a drive.
using IWshRuntimeLibrary;
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:");
I have documented the detailed steps here