Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
660 views
in Technique[技术] by (71.8m points)

c# - Check if directory exists on Network Drive

I'm trying to detect if the directory exists, but in this particular situation my directory is a network location. I used VB.NET's My.Computer.FileSystem.DirectoryExists(PATH) and the more general System.IO.Directory.Exists(PATH), and in both cases, the system response is false. I checked and the PATH exists, I can view it in MyComputer Folder. If I debug the program, and watch the My.Computer.FileSystem.Drives variable, the network location does not appear in that list.

UPDATE: I checked and in Windows XP the Response is True, but not in Windows 7.

UPDATE2: I tested both proposed solutions but I still have the same problem, on the image below you will see that I can access using Explorer but my program cannot. The GetUNCPath function returns a valid path (no errors), but Directory.Exists stil returns false.

I also tried with the UNC path "\ServerImages"; same result.

enter image description here

UPDATE3: If I cannot link with a network drive, how can I link to UNC path directly?. I discovered that if i run VS in normal mode, it works, but my software must run in administrator mode. So, there is any way to check the existence of a network directory as administrator?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If UAC is turned on, mapped network drives only exist "by default" in the session they are mapped: Normal or elevated. If you map a network drive from explorer, then run VS as admin, the drive will not be there.

You need to enable what MS calls "linked connections": HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersionPoliciesSystem: EnableLinkedConnections (REG_DWORD) = 0x1

Background Information about "two logon sessions" with UAC: http://support.microsoft.com/kb/937624/en-us


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...