简体   繁体   中英

My C# code can't see my directory in my C drive…even though the directory exists

My directory is at C:\\Testing\\Event Log 123

The 123 portion is a timestamp. My code runs and generates a timestamped directory in C:\\Testing upon completion. I have this piece of code that checks if that directory exists:

string dirToCopy = @"C:\Testing\Event Log " + timestamp;
if (System.IO.Directory.Exists(dirToCopy))
{
   APILog.AddMessage("Event System log directory found.");
}
else
{
   APILog.AddMessage("Event System log directory not found.");
 }      

The directory does exist at that location, but the else statement's log message is what gets displayed. I don't think there's an issue with permissions, as I'd be getting a security exception if that was the case...so why can't my code see the directory that I can see with my own eyeballs right now? I tried outputting dirToCopy to make sure that it matches the directory's actual name. They match, so I'm surprised that my code doesn't see it.

Edit for more info: My code runs on a client PC. It generates the directory and pastes it into the main PC's C:\\Testing directory. The main PC's C:\\Testing directory is a sort of shared directory that the client can also access. Does this matter, though? C:\\Testing is on the main PC, and I'm running the code on the main PC.

So the directory You're looking for is "C:\\Testing\\Event Log2016.01.26" or maybe "C:\\Testing\\Event Log\\2016.01.26"

the point being - the slash at the end ? isn't it missing ?

you could also consider using Path.Combine() for building the path string

This is what MSDN says

If you do not have at a minimum read-only permission to the directory, the Exists method will return false.

https://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx

So it does not throws security exception....so do check once if you have read permission on that directory

Update

If it is shared directory then your path should look like this

string dirToCopy = @"\\MainPC\Testing\Event Log 123" (Assuming testing folder is shared)

If it is UNC path then your path should like this

string dirToCopy = @"\\MainPC\C$\Testing\Event Log 123"

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