简体   繁体   English

即使目录存在,我的C#代码也无法在C驱动器中看到我的目录。

[英]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 我的目录位于C:\\ Testing \\ Event Log 123

The 123 portion is a timestamp. 123部分是时间戳。 My code runs and generates a timestamped directory in C:\\Testing upon completion. 我的代码运行并在完成后在C:\\ Testing中生成带有时间戳的目录。 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. 该目录确实存在于该位置,但是else语句的日志消息就是显示的内容。 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. 我尝试输出dirToCopy以确保它与目录的实际名称匹配。 They match, so I'm surprised that my code doesn't see it. 它们匹配,所以令我惊讶的是我的代码没有看到它。

Edit for more info: My code runs on a client PC. 编辑以获得更多信息:我的代码在客户端PC上运行。 It generates the directory and pastes it into the main PC's C:\\Testing directory. 它生成目录并将其粘贴到主PC的C:\\ Testing目录中。 The main PC's C:\\Testing directory is a sort of shared directory that the client can also access. 主PC的C:\\ Testing目录是客户端也可以访问的一种共享目录。 Does this matter, though? 这有关系吗? C:\\Testing is on the main PC, and I'm running the code on the main PC. C:\\ Testing在主PC上,并且我在主PC上运行代码。

So the directory You're looking for is "C:\\Testing\\Event Log2016.01.26" or maybe "C:\\Testing\\Event Log\\2016.01.26" 因此,您要查找的目录是"C:\\Testing\\Event Log2016.01.26""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 您也可以考虑使用Path.Combine()构建路径字符串

This is what MSDN says 这就是MSDN所说的

If you do not have at a minimum read-only permission to the directory, the Exists method will return false. 如果您没有目录的最低只读权限,则Exists方法将返回false。

https://msdn.microsoft.com/en-us/library/system.io.directory.exists.aspx https://msdn.microsoft.com/zh-CN/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 如果它是UNC路径,那么您的路径应该像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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