[英]How to check a file is present or not in UNC path?
Im trying to check the presence of the file in a UNC path using the File.Exist() func in c#, But even if the file is present it returns only as false.我试图使用 c# 中的 File.Exist() 函数检查 UNC 路径中文件是否存在,但即使文件存在,它也只会返回为 false。
Following is the sample code以下是示例代码
outPath="\\DevSrv\\outPath\\result.txt";
if(File.Exists(outPath))
{
.....
}
The above code always fails event the path "\DevSrv\outPath\result.txt" is valid.如果路径“\DevSrv\outPath\result.txt”有效,上述代码总是失败。 I am able to access the path using the windows explorer.我可以使用 windows 资源管理器访问路径。
Sorry i missed the "\" while creating this post抱歉,我在创建此帖子时错过了“\”
Use @
before your string for avoid having to escape slashes 在字符串前使用@
,以避免必须转义斜线
outPath=@"\\DevSrv\outPath\result.txt";
if(File.Exists(outPath))
{
.....
}
You aren't escaping the slashes, try: 您没有逃脱斜线,请尝试:
outPath="\\\\DevSrv\\outPath\\result.txt";
if(File.Exists(outPath))
{
.....
}
When using strings with a backslash you need to think about escape sequence. 当使用带有反斜杠的字符串时,您需要考虑转义序列。 This link will give you a detailed view about the escape sequences. 该链接将为您提供有关转义序列的详细视图。 http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-character-escape-sequences-are-available.aspx http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-character-escape-sequences-are-available.aspx
Quote from link : 引用链接:
C# defines the following character escape sequences: C#定义了以下字符转义序列:
Of these, \\a, \\f, \\v, \\x and \\U are rarely used in my experience. 在我的经验中,很少使用\\ a,\\ f,\\ v,\\ x和\\ U。
So in your example. 因此,在您的示例中。 You can use the following options for your string. 您可以对字符串使用以下选项。
outPath=@"\\DevSrv\outPath\result.txt";
//Or
outPath="\\\\DevSrv\\outPath\\result.txt";
There's a difference between UNC and regular paths. UNC 和常规路径之间存在差异。
considuring having a file:考虑有一个文件:
c:\temp\myFile.txt c:\temp\myFile.txt
Testing for测试
File.Exists(@"c:\temp\myFile.txt")
will return true.将返回真。
But even the following (having double backslashes)但即使是以下(有双反斜杠)
File.Exists(@"c:\temp\\myFile.txt")
returns true.返回真。
BUT using UNC + having doubleSlashes但是使用 UNC + 有双斜杠
File.Exists(@"\\?\c:\temp\\myFile.txt")
returns false返回假
So - as many mentioned - using所以 - 正如许多人提到的 - 使用
Path.Combine()
is a good habit since it makes sure you don't have these double slashes.是一个好习惯,因为它可以确保你没有这些双斜线。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.