Fileinfo错误“太长的路径“ [英] Fileinfo error " too long path"

查看:99
本文介绍了Fileinfo错误“太长的路径“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨每一个



我正在尝试获取有关指定目录中文件的信息



so我正在使用此代码

hi every one

I am trying to get information about files in specified Directory

so i am using this code

string path = @"D:\Directory";
            string[] filepathes = Directory.GetFiles(path, ".", SearchOption.AllDirectories);
            var items = from f in filepathes
                        select new ListViewItem(
                            new string[] {
                            new FileInfo(f).Directory.ToString(),
                            new FileInfo(f).Name.ToString(),
                            new FileInfo(f).CreationTime.ToString(),
                            new FileInfo(f).Length.ToString()});





但我收到错误

指定的路径,文件名或两者都太长了。完全合格文件名必须少于260个字符,并且dir ectory名称必须小于248个字符。



我知道当文件路径太长时会发生此错误



但我的问题是有什么方法可以避免这个错误





感谢所有



but I got error
"The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."

I know this error happens when file path too long

but my question is there any way to avoid this error


thanks to all

推荐答案

这是.net框架调用的API函数的限制。这注意到,Windows或.net无法处理这样的长文件名。只是内置工具是有限的。很遗憾,但就是这样:(

你可以使用库。就像这些:

http://zetalongpaths.codeplex.com/ [ ^ ]

http://bcl.codeplex.com/wikipage?title = Long%20Path& referTitle = Home [ ^ ]

http://gallery.technet。 microsoft.com/DelimonWin32IO-Library-V40-7ff6b16c [ ^ ]

只是一个注释:它们可能不是绝对完整的。我建议你寻找更多,尝试尽可能多的人找到最适合你。
That is a limitation of the API functions what the .net framework is calling. This does note mean, that Windows or .net can not handle such long file names. Just the built-in tools are limited. Shame, but that's it :(
There are libraries out there you can use. Like these:
http://zetalongpaths.codeplex.com/[^]
http://bcl.codeplex.com/wikipage?title=Long%20Path&referringTitle=Home[^]
http://gallery.technet.microsoft.com/DelimonWin32IO-Library-V40-7ff6b16c[^]
Just a note: they might be not absolutely complete. I suggest you look for more, try out as many as you can and find the most suitable for you.


获取文件名太长的文件信息的方法是P / Invoke到Windows API。我相信Zoltán链接到的图书馆将在幕后进行(实际上第一个甚至链接到一些关于它的页面),所以我不会在这里添加这些信息。



我假设当您尝试创建FileInfo而不是获取文件列表时发生异常。在这种情况下,您已经拥有路径,因此您可以捕获异常,或者在尝试构造FileInfo之前检查路径的长度,并在这种情况下执行其他操作 - 例如从列表中排除路径,或报告路径太长,或尝试通过P / Invoke(或使用Zoltán提到的库)获取所需信息。



鉴于您正在尝试获取子目录中文件的所有信息,如果权限被拒绝等,您应该也可以做一些事情,所以我会去捕获异常。 (除此之外,检查文件路径是否超出长度限制比简单检查其长度更复杂,因为路径和文件名长度都很重要。这样的检查只会减慢循环)。



此外,您要为每个文件构建FileInfo四次。我相信每个实例都会在构造时缓存文件的许多属性,因此这将不必要地缓慢。 (请参阅 FileInfo 示例中的注释[ ^ ])



考虑到这一切,无论路径问题太长,我都会认真考虑修改这段代码。



我是害怕我对你所使用的选择语法一无所知,所以如果我要重做这个,我可能会做这样的事情:



The method of getting information about files whose filename is too long is to P/Invoke to the Windows API. I believe that's what the libraries which Zoltán has linked to will be doing under the hood (in fact the first one even links to some pages about it), so I won't add to that information here.

I assume the exception happens when you try to create the FileInfo and not when getting the list of files. In which case you already have the paths, so you can either trap the exception, or check the length of the path before attempting to construct the FileInfo, and do something else in that case - e.g. exclude the path from your list, or report that the path is too long, or try to get the required information through P/Invoke (or by using a library mentioned by Zoltán).

Given that you're trying to get all the information of files in the sub-directory, you should probably also do something if permissions are denied to you, etc, so I'd go with the trapping the exception. (Besides which, checking whether the file path exceeds the length restrictions is more complicated than simply checking its length, since both the path and the filename length that matters. Such checks will just slow down the loop).

Additionally, you are constructing FileInfo for each file four times. I believe each instance will cache many of the properties of the file on construction, so this will be unnecessarily slow. (See the note in the Examples of FileInfo[^])

With all this in mind, regardless of the path too long problem, I'd seriously consider modifying this code.

I'm afraid I don't know anything about "select" syntax that you've used, so if I was to rework this I'd probably do something like this:

string path = @"D:\Directory";
string[] filePaths = Directory.GetFiles(path, ".", SearchOption.AllDirectories);
foreach (string f in filePaths)
{
  try
  {
    FileInfo info = new FileInfo(f);
    ListViewItem item = new ListViewItem(new string[] {
      info.DirectoryName,         // Note use of this instead of Directory.ToString() !!
      info.Name.ToString(),
      info.CreationTime.ToString(),
      info.Length.ToString()});
    // Do what you need to do with "item"
  }
  catch(PathTooLongException)
  {
    // Ignore, or add listview item to report error, or
    // do something to try to get the information
    // (e.g. P/Invoke to the Windows API).
    // Note since you already have the full path name, you could use
    // System.IO.Path to get the directory and file name.
    // E.g.
    ListViewItem item = new ListViewItem(new string[] {
      Path.GetDirectoryName(f),
      Path.GetFileName(f),
      "Path too long",   /* Error message in "Created Time" field.  This is only an example, you probably don't want to do this in real application! Maybe have an additional "error" field? */
      ""});
    // Do what you need to do with "item"
  }
  catch(SecurityException)
  {
    // Ignore, or add listview item reporting permission denied, as above.
  }
  catch(UnauthorizedAccessException)
  {
    // Ignore, or add listview item reporting permission denied, as above.
  }
}



问候,

Ian。


Regards,
Ian.


非常感谢所有



像ZoltánZörgő说的我用了



Delimon.Win32.IO.dll库而不是system.IO和It Worked Fine



最终代码是:



thanks a lot to all

like Zoltán Zörgő said I used

Delimon.Win32.IO.dll library instead of system.IO and It Worked Fine

the Final Code Is :

string path = @"D:\books";
            string[] filepathes = System.IO.Directory.GetFiles(path, ".", System.IO.SearchOption.AllDirectories);
var items = from f in filepathes
select new ListViewItem(
new string[] {
new Delimon.Win32.IO.FileInfo(f).Directory.FullName, // Directory
new Delimon.Win32.IO.FileInfo(f).Name.ToString(), // Name
new Delimon.Win32.IO.FileInfo(f).CreationTime.ToString(),
new Delimon.Win32.IO.FileInfo(f).Length.ToString()});


这篇关于Fileinfo错误“太长的路径“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆