C# 打开网络文件(UNC)

本文探讨了UNC路径在C#中的正确使用方法,并解释了为何在HTML中使用相同路径会遇到问题。通过示例说明了如何在C#中正确指定UNC路径,并指出在不同编程语言中使用路径时需要注意的细节。

比如一个   UNC 路径为   

  string path = @"\\WUMI_LIU_PC1\Users\1.pdf";

 string[] readText = File.ReadAllLines(path, Encoding.UTF8);  //这样就可以读取到  path 路径下的文件了。

这是在C# 中的使用。

按照这个思路, 在 htm 中 也这么使用这个路径。

发现 打不开这个路径的文件。

 

后来通过调试,发现在C# 中, string path = @"\\WUMI_LIU_PC1\Users\1.pdf";

 其实是 \\\\WUMI_LIU_PC1\\Users\\1.pdf  前面使用四个斜杠的。

 msdn中这么描述的:

In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all the following are acceptable paths:

  • "c:\\MyDir\\MyFile.txt" in C#, or "c:\MyDir\MyFile.txt" in Visual Basic.

  • "c:\\MyDir" in C#, or "c:\MyDir" in Visual Basic.

  • "MyDir\\MySubdir" in C#, or "MyDir\MySubDir" in Visual Basic.

  • "\\\\MyServer\\MyShare" in C#, or "\\MyServer\MyShare" in Visual Basic.

地址:http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(SYSTEM.IO.FILE);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&rd=true

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值