简体   繁体   English

如何检查文件是否存在于 UNC 路径中?

[英]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-c​​haracter-escape-sequences-are-available.aspx

Quote from link : 引用链接:

C# defines the following character escape sequences: C#定义了以下字符转义序列:

  • \\' - single quote, needed for character literals \\'-单引号,字符文字需要
  • \\" - double quote, needed for string literals \\“-双引号,字符串文字需要
  • \\\\ - backslash \\\\-反斜杠
  • \\0 - Unicode character 0 \\ 0-Unicode字符0
  • \\a - Alert (character 7) \\ a-警报(字符7)
  • \\b - Backspace (character 8) \\ b-退格键(字符8)
  • \\f - Form feed (character 12) \\ f-换页(字符12)
  • \\n - New line (character 10) \\ n-换行符(字符10)
  • \\r - Carriage return (character 13) \\ r-回车符(字符13)
  • \\t - Horizontal tab (character 9) \\ t-“水平”标签(字符9)
  • \\v - Vertical quote (character 11) \\ v-垂直引号(字符11)
  • \\uxxxx - Unicode escape sequence for character with hex value xxxx \\ uxxxx-具有十六进制值xxxx的字符的Unicode转义序列
  • \\xn[n][n][n] - Unicode escape sequence for character with hex value nnnn (variable length version of \\uxxxx) \\ xn [n] [n] [n]-十六进制值为nnnn的字符的Unicode转义序列(\\ uxxxx的可变长度版本)
  • \\Uxxxxxxxx - Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates) \\ Uxxxxxxxx-十六进制值为xxxxxxxx的字符的Unicode转义序列(用于生成代理)

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.

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