C# 代码中的 PathTooLongException
我有以下代码:
public static void Serialize()
{
List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
List<string> dirFiles = new List<string>();
foreach (string p in dirs)
{
string path = p;
string lastAccessTime = File.GetLastAccessTime(path).ToString();
bool DirFile = File.Exists(path);
FileInfo fInf = new FileInfo(path);
long lengthInk = fInf.Length / 1024;
DateTime lastWriteTime = File.GetLastWriteTime(p);
dirFiles.Add(p + "|" + lastAccessTime.ToString() + "|" + DirFile.ToString() + "|" + lastWriteTime.ToString() + "|" + lengthInk.ToString() + " kb");
}
我不断遇到 PathTooLongException 错误,其中包含以下行:
string lastAccessTime = File.GetLastAccessTime(path).ToString();
应用程序钻入驱动器并查找驱动器中的所有文件/文件夹。 我无法更改此路径,但由于它超过 260 个字符...如何解决此问题?
i have the following code:
public static void Serialize()
{
List<string> dirs = FileHelper.GetFilesRecursive(fileDirectoryPath);
List<string> dirFiles = new List<string>();
foreach (string p in dirs)
{
string path = p;
string lastAccessTime = File.GetLastAccessTime(path).ToString();
bool DirFile = File.Exists(path);
FileInfo fInf = new FileInfo(path);
long lengthInk = fInf.Length / 1024;
DateTime lastWriteTime = File.GetLastWriteTime(p);
dirFiles.Add(p + "|" + lastAccessTime.ToString() + "|" + DirFile.ToString() + "|" + lastWriteTime.ToString() + "|" + lengthInk.ToString() + " kb");
}
I keep hitting a PathTooLongException error with the following line:
string lastAccessTime = File.GetLastAccessTime(path).ToString();
The application drills into a drive and finds all files/folders w/in the drive. I cannot change this path but since it is above 260 characters...how to work around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如微软所说这里, Windows 限制为 260 个字符。
您可以尝试使用符号链接(不确定...)。
As Microsoft says here, there is a Windows limitation on 260 characters.
You can try to avoid this with a symbolic link (not sure...).
.NET 不支持 Unicode 文件路径,因此我知道在这种情况下唯一的选择是使用 P/Invoke(当然,除非您可以更改路径)来调用支持它们的 Win32 API 函数。 您可以在此处查看说明关于如何使用 Unicode 文件路径来打破 260 个字符的障碍。
.NET doesn't support Unicode file paths, so the only option I know of in this case is using P/Invoke (unless, of course, you can change the path) to call Win32 API functions that do support them. You can look here for instructions on how to use Unicode file path to break the 260 characters barrier.
具有完整路径的
GetLastAccessTime()
调用可以 超出完全限定文件路径最大长度的内部限制(特定于操作系统版本,但通常为 260 个字符)。避免这种情况的一种方法是使用
Directory.SetCurrentDirectory()
更改当前系统目录,然后仅使用相对路径调用GetLastAccessTime()
。 只需确保将当前目录更改回起始目录即可,以避免出现意外问题。The
GetLastAccessTime()
call, with a full path can exceed the internal limit (which is OS-version specific, but typically 260 characters) on the maximum length for a fully qualified file path.One way to avoid this, is to use
Directory.SetCurrentDirectory()
to change the current system directory and then callGetLastAccessTime()
with only a relative path. Just make sure you change your current directory back to what you started from to avoid unexpected issues.像 Delimon.Win32.IO.FileInfo 的
.LastAccessTime
属性之类的东西可能会起作用。Delimon 是 Microsoft TechNet 上的一个用于克服长文件名问题的库,它称为 Delimon.Win32.IO 库 (V4.0) 并且它有自己版本的 System.IO 关键类。
例如,您可以将: 替换
为
,这样您就可以处理长文件和文件夹。
来自网站:
Something like the
.LastAccessTime
property of Delimon.Win32.IO.FileInfo, might do the trick.Delimon is a library on Microsoft TechNet for overcoming the long filenames problem, it's called Delimon.Win32.IO Library (V4.0) and it has its own versions of key classes from System.IO
For example, you would replace:
with
which will let you handle long files and folders.
From the website: