Ahmed Atia
Ahmed Atia

Reputation: 17970

How does one map network drive from windows service?

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?

Upvotes: 3

Views: 22550

Answers (7)

David L Morris
David L Morris

Reputation: 1513

There are two issues:

  1. Mappings are only in use for the user session, which means that effectively you can't use a mapped drive for a Service. You would need to use the path.
  2. The second issue is that a service (using the local system account) does not have access to the network, or more specifically, to the resource required. To resolve this, you would need to, either: Give the 'computer' on which the service is running specific access to the folder, or, set up the service to use a network (DOMAIN) account that has access to the resource.

Upvotes: 1

Vimal Raj
Vimal Raj

Reputation: 1038

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

Upvotes: 5

Ahmed Atia
Ahmed Atia

Reputation: 17970

All issues solved by using Map Network Drive (API) to map network drive. I map required drives while OnStart event of service.

Upvotes: 2

Winston Smith
Winston Smith

Reputation: 21922

As far as I know, a mapped drive is only mapped for the duration of the user session. If your windows service is running on startup, or is otherwise not part of a user session, the drive mapping is lost.

This may or may not be true but it is worth looking into - I remember something about it from a similar scenario I had about 7 years ago.

Upvotes: 0

Andomar
Andomar

Reputation: 238276

I think services can't map network drives unless they have the right "interact with desktop" ?

Try to run the service under the "Network Service" account, instead of "Local System" ?

Upvotes: 0

MSalters
MSalters

Reputation: 180155

It might be better to just call the right API function, instead of calling a batch file to call another executable. The function you're looking for is DefineDosDevice()

Upvotes: 1

MSalters
MSalters

Reputation: 180155

Are you running the service under the user account that belongs to the password? The MAP USE command will use the current user, unless you pass /USER:anotheruser

Upvotes: 0

Related Questions