简体   繁体   中英

Directory.exists returns false for mapped drive in c# coding

I am using Directory.Exists() in my windows service (that is programmed in C#, 3.5 framework)to check to see whether a particular directory exists in the drive. When I run in local machine it works fine, meaning I am able to access the directory.

But when I deploy the windows service on a Virtual Machine, and start the service, it is not able to find the directory even though the directory exists. The directory is mapped on as

 Q: drive, Q:\\temp\\local\\ folder 

But the windows services always returns false for the Directory.Exists().

However when I give C:\\ drive in place of Q:\\ it works, but does not work for a mapped drive. I have tried with the UNC path, and I have made sure the mapped drive have the administrative rights and infact the read, write and execute permission. But it still returns false.

Can anyone please tell me why? And how to resolve?

Make sure the drive is mapped under the same user as the Service is running. If you map the drive as user A, it is not automatically mapped for anyone else too.

Mapped drives are only restored during interactive login which services generally do not perform:

Map a network drive to be used by a service

Short version: You can't do it, use the full UNC path instead.

This is most probably a problem with privileges. Your Windows service is probably running under an account which doesn´t have enough privileges to access the network path.

This is a possible duplicate: Accessing mapped folder from a Windows Service written in C#

Another possible solution is to use impersonation, check it out: http://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.90).aspx

UPDATE

Came to think of it; Try changing the identity of the application pool to a user with the same rights as your user.

As @Sriram pointed out the Directory.Exists() method will fail if any error occurs. What sort of exception do you get if you try to access the path?

Eg (for both mapped and UNC in case there is something going on there):

DirectoryInfo diMapped = new DirectoryInfo(@"Q:\temp\local\folder");
DirectoryInfo diUNC = new DirectoryInfo(@"\\servername\fnsw\tmp\126");

Note: Assuming that the white space before 'folder' in your path is a typo?

Steps to troubleshoot

  1. Try accessing the network path manually in "Run" [WindowKey + R]
  2. Try to access your map drive ie: M:\\
  3. Make sure you are the account owner of the mapping (mapping should be done under your account)
  4. Go to Property and see if "Run As Administrator" is unchecked.
  5. Remove mapping and re-add the mapping.
  6. Make sure available offline (or sync offline) is turned off and folder is available from another computer.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM