C# 文件未找到异常.. 但它在那里

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1350076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:30:39  来源:igfitidea点击:

File not found Exception.. But it's there

c#file.net

提问by Nick

Hey this is going to be one of those dumb questions. I am trying to pick up a file on my local system and I keep getting a FileNotFoundExceptionthrown.

嘿,这将是那些愚蠢的问题之一。我试图在我的本地系统上获取一个文件,但我一直被FileNotFoundException抛出。

Someone set me straight please :)

请有人让我直截了当:)

if( File.Exists(@"C:\logs\hw-healthways-prod_2009-08-26.tar"))
{
     Console.WriteLine("Yay");
}
else
{
     throw new FileNotFoundException();   
}

Tried moving the file into the same location as the executing application and did the following:

尝试将文件移动到与正在执行的应用程序相同的位置并执行以下操作:

 if( File.Exists("hw-healthways-prod_2009-08-26.tar"))

Same thing.

一样。

Then I made a random txt file and parked it there too.. "me.txt"

然后我制作了一个随机的txt文件并将其停在那里..“me.txt”

And it works?! So you thing the file name is the problem?

它有效吗?!所以你的文件名是问题?

采纳答案by Daniel Pryden

Try doing Directory.GetFiles(@"C:\logs"). It's possible that the file in question has odd characters that are getting interpreted one way by Windows Explorer (presumably where you're reading "the file's property" from?) but a different way by the .NET Framework. This can happen if you have UTF-8 characters in the filename (perhaps an en dash?).

尝试做Directory.GetFiles(@"C:\logs")。有问题的文件可能有奇怪的字符,这些字符会被 Windows 资源管理器以一种方式解释(大概是您从哪里读取“文件的属性”?)但 .NET 框架以不同的方式解释。如果文件名中包含 UTF-8 字符(也许是破折号?),就会发生这种情况。

回答by user161433

You may want to check your file permissions. Your computer may not have permission to the file.

您可能需要检查您的文件权限。您的计算机可能没有该文件的权限。

回答by user37875

C:\logs\hw-healthways-prod_2009-08-26.tar should be C:\\logs\\hw-healthways-prod_2009-08-26.tar. \ means the next character is an escape character.

C:\logs\hw-healthways-prod_2009-08-26.tar 应该是 C:\\logs\\hw-healthways-prod_2009-08-26.tar。\ 表示下一个字符是转义字符。

回答by i31nGo

May be the name of the file is "hw-healthways-prod_2009-08-26.tar.tar" instead of "hw-healthways-prod_2009-08-26.tar", I had this problem because by default the extension files are hidden on windows

可能是文件名是“hw-healthways-prod_2009-08-26 .tar.tar”而不是“hw-healthways-prod_2009-08-26 .tar”,我遇到了这个问题,因为默认情况下扩展文件是隐藏在窗户上