开发者

File system paths and Directory.Exists()

I'm at a bit of a loss on this one ... can't seem to understand what is going on.

I have an app that writes some files to a configured output directory, but before that, when the application loads I do this ...

string path = ConfigurationManager.AppSettings["TempDir"];
if (!Directory.Exists(path))
    Directory.CreateDirectory(path);

My problem is that in my case the path is a network based location "\MyServer\Share" and when i use that path it works, but the path i'm using is actually "Z:\" because the share is mapped and is automatically mapped in all user sessions across the network.

I want to use "Z:\" but it doesn't work throwing back an error saying "all or part of the path doesn't exist".

I immediately thought "oh this must be permissions" so i checked it against say "G:\" which worked and has the same permissions applied ...

For all shares in question my user account has full control permissions on the location.

Initially i thought it might be something like this: Directory.Exists not getting mapped directory But then i remembered that it couldn't be that the app is running under another account because I checked and

  1. its only a console app
  2. the line before is this:

    AppDomain.CurrentDomain.SetThreadPrincipal(new WindowsPrincipal(WindowsIdentity.GetCurrent()));
    

.. as i understand the purpose of this is to ensure that the appdomain is running under the context of the current windows user, that includes all "threads created within the app domain" unless otherwise specified

Any ideas?

EDIT:

Looking through process monitor as Richard suggests ( see comments below) shows that if i use the full unc path "\MyServer\Share" the request is made as I would expec开发者_StackOverflow中文版t, if i use the mapped path "Z:\" it still requests the full unc path "\MyServer\Share" (presumably it is doing some form of translation in the inner workings of .net).

so no matter what i'm requesting a full unc path "\MyServer\Share" and yet only by explicitly specifying the full unc path can i access it ... weird !!!

The code posted above is exactly as I have written it in my console application, there's no other stuff going on yet other than that principle thing as these are the first lines of code to execute in my console application.

Hmmm ...

EDIT 2:

Ok now i'm really confused, when put a breakpoint on the configuration file request line above and run it up to the breakpoint then clear out process monitor and step over that and the if statement nothing appears in the process monitor as I would expect it to ...

Why would that happen?


You can not use mapped drive as a path you need to make a shared directory and give it the permission you need (read or write) and then connect to it like this: \ServerName\SharedDirectoryName\OneMoreDirectory


I seem to have found an odd bug in the debugging tools / .net framework ... When the debugger is attached to my app it fails, when the debugger is not attached it works as expected.

The recommendation is always to use a full unc path but the calls to Directory.Exists(path) and Directory.Delete(path) resolve mapped drives before making the call as process monitor reveals so although it is recommended it is not required.

I have found however that running the app then attaching visual studio manually seems to allow the app to run as epected and allow the require debugging in my situation.

This solution comes with the "Works on my PC garantee" only ... i take no responsibility for anything said in this question :)

Thank you to everyone for the help however I seem to have resolved it ... UAc was good advice and so was process monitor ... thanks guys ... really helped :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜