我有两条路:
\\10.11.11.130\FileServer\Folder2\Folder3\
\\10.11.11.130\d$\Main\FileServer\Folder2\Folder3\
Run Code Online (Sandbox Code Playgroud)
我想检测两条路径是否相同.
我想要它,因为我试图将一个文件移动到另一个目录.因此,对于上面的路径,会抛出异常.
我知道我可以使用try和catch,但还有另外一种方法吗?
我想d$\Main从第二条路径中移除然后进行比较,但并不总是如此......
任何帮助赞赏!
您可以使用这样的方法来检查是否相等:
public static bool PathsSame(string pth1, string pth2)
{
string fName = System.IO.Path.GetRandomFileName();
string fPath = System.IO.Path.Combine(pth1, fName);
System.IO.File.Create(fPath);
string nPath = System.IO.Path.Combine(pth2, fName);
bool same = File.Exists(nPath);
System.IO.File.Delete(fPath);
return same;
}
Run Code Online (Sandbox Code Playgroud)
这模拟了检查路径是否相同的行为,您可以创建一个具有唯一名称的文件并检查它是否存在于其他目录中。然后您可以删除创建的文件,因为不再需要它。这不是最好的解决方案,但可能就足够了。
这也不能处理可能发生的错误。有关错误处理,请参见:https://msdn.microsoft.com/en-us/library/vstudio/as2f1fez (v=vs.110).aspx
| 归档时间: |
|
| 查看次数: |
757 次 |
| 最近记录: |