Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does DriveInfo.GetDrives() not get network mapped drive in windows service

I have a windows service developed in .NET C# using VS 2010, I'm calling the DriveInfo.GetDrives() but it is not getting the Z: drive (a mapped network drive),

I did some googling and found some results pointing to windows account privileges, so I have tried all account types of service installer such as LocalSystem, User, LocalService, NetworkService but none worked and I still can't get the drive Z:.

I did another test, debugging the windows service (as a console application) and I can get drive Z: that way.

Is there a solution to my problem?

like image 606
khaled jendi Avatar asked Aug 19 '12 20:08

khaled jendi


People also ask

Why can't I see my mapped network drives?

The primary reason for mapped network drives not showing in all folders is because all the folders are hidden in the NAS and other drives.

Can a Windows Service access a mapped drive?

Drive mappings exist across logon contexts, so if an application is running in the context of the LocalService account, then any other service running in that context may have access to the mapped drive.

How do I get my network drive to show up?

Open File Explorer from the taskbar or the Start menu, or press the Windows logo key + E. Select This PC from the left pane. Then, on the Computer tab, select Map network drive. In the Drive list, select a drive letter.


2 Answers

You need to run the windows service with an account that has the network drive mapped, for example, the same account that you used to run as a console application. As Hans advised you should not be using mapped drives in windows services because they are a concept associated to a real user.

However if you really want to continue to use mapped drives see this related question for pitfalls and workarounds related to this specific situation:

How to map a network drive to be used by a service

like image 175
João Angelo Avatar answered Oct 08 '22 20:10

João Angelo


Drive mappings are associated with LUID and you could have multiple Authentication ID under the same user (e.g. service, normal integrity level, high integrity level etc).

You can have a normal integrity level process running in the user's session to provide the mapped drive list created by the user. This is how Windows Explorer copies mapped drives for elevated setup programs.

like image 26
Sheng Jiang 蒋晟 Avatar answered Oct 08 '22 18:10

Sheng Jiang 蒋晟