是否有.NET API返回给定路径相当于扩展路径字符串?路径、字符串、NET、API

2023-09-05 01:29:15 作者:唯美

在NTFS,我可以preFIX与的路径\\。\ 字符序列来表示,这是超过260个字符的限制的路径?;因此,该文件系统将跨preT正确的道路,避免引发 PathTooLongException

(见 HTTP:/ /msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath 获得更多信息)的

是否有.NET API,将preFIX我的路径字符串与此序列,还是我坚持写我自己的?

在本质上,我在寻找一种方法,就是等同于以下。

 静态字符串ToExtendedPath(字符串路径)
{
    如果(Path.IsPathRooted(路径))
    {
        返回@\\。\?+路径;
    }

    返回Path.Combine(@\\?\,路径);
}
 
day10 API 字符串

解决方案

没有,没有.NET API的转换给定的正常的路径进入扩展语法。你必须推出自己的(这是微不足道的,顺便说一句)。

请注意:由于科迪灰色和汉斯帕桑特提到的,.NET框架不支持长(扩展)的路径。如果你想与他们合作,你需要直接使用API​​。而且也不是所有的API函数支持长路径无论是。一般情况下,低级别的功能做的。请参考MSDN文档。

我所做的就是写的包装功能相关的API函数(如的CreateFile)和调用的.NET文件系统功能,这些包装来代替。

In NTFS, I can prefix a path with the \\?\ character sequence to denote that it is a path that exceeds the 260-character limit; as such, the file system will interpret the path correctly and avoid raising PathTooLongException.

(see http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx#maxpath for more information)

Is there a .NET API that will prefix my path strings with this sequence, or am I stuck writing my own?

In essence, I am looking for a method that is equivalent to the following.

static string ToExtendedPath(string path)
{
    if(Path.IsPathRooted(path))
    {
        return @"\\?\" + path;
    }

    return Path.Combine(@"\\?\", path);
}

解决方案

No, there is no .NET API that translates a given "normal" path into the extended syntax. You have to roll your own (which is trivial, by the way).

Please note: As Cody Gray and Hans Passant mentioned, the .NET framework does not support long (extended) paths. If you want to work with them, you need to use the API directly. And not all API functions support long paths either. Generally, the low-level functions do. Consult the MSDN documentation.

What I have done is write wrapper functions for the relevant API functions (e.g. CreateFile) and call those wrappers instead of the .NET file system functions.