GetDirectories无法枚举#255名称的文件夹的子文件夹 [英] GetDirectories fails to enumerate subfolders of a folder with #255 name

查看:154
本文介绍了GetDirectories无法枚举#255名称的文件夹的子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是C#3.5在Windows 7旗舰版,64位上运行。它通过所有文件夹子文件夹来执行其作业。然而,它失败(陷入无限循环,直到StackOverflow.com异常),如果运行的文件夹名称是只有一个符号是#255。

重现,您可以执行以下操作:
$ b


  1. 运行Windows资源管理器在该文件夹中创建C:\Temp文件夹
  2. 创建新文件夹并用Alt-255(使用数字键盘)重新命名它。

  3. 创建子文件夹first和second子文件夹1和2下的临时

所以你现在有:

C:\ 1

  • C:\ 2

  • C:\ \\\ first

  • C:\\ second



  • 对于C:\ Temp文件夹子文件夹名称#255(或更多#255符号)以下代码:

      using System; 
    using System.Collections.Generic;
    使用System.Linq;
    使用System.Text;
    使用System.IO;


    {
    public static string [] GetDirectories(string pathToTraverse)
    {
    List< string> result = new List< string>();

    foreach(DirectoryInfo子文件夹在新的DirectoryInfo(pathToTraverse).GetDirectories())
    {
    result.Add(subFolder.FullName);
    }
    return result.ToArray();

    $ b $ public static void TraverseFolders(string folderToTraverse)
    {
    foreach(GetDirectories(folderToTraverse)中的字符串子文件夹)
    {
    Console .WriteLine(子文件夹);

    TraverseFolders(subFolder);


    $ b static void Main(string [] args)
    {
    TraverseFolders(@C:\Temp);




    $ b $ p $永远不会结束, / p>

    \Temp\

    C:\Temp\1

    C:\Temp\ 2

    C:\ Temp \

    C:\Temp\1

    C:\Temp\2

    C:\Temp\

    C:\Temp\1

    C:\Temp\2

    C:\ Temp \


    那么如何正确地枚举这样的文件夹子文件夹呢? / b>下面的程序运行完美,不会导致堆栈溢出错误。

      using System; 
    使用System.Text;
    使用System.IO;

    命名空间ConsoleApplication1
    {
    类程序
    {
    static void Main(string [] args)
    {
    string pathToTraverse = @C:\桌面;
    foreach(DirectoryInfo子文件夹在新的DirectoryInfo(pathToTraverse).GetDirectories())
    {
    System.Console.WriteLine(subFolder);



    $ b code


    产生以下输出:

    $ $ $ $ b $

    $ b $ _ $ h $
    $ b $

    倒数第二个显然是空白的行实际上是名为Alt + 255的目录。 b

    因此,我相信你的问题与你所显示的代码无关,事实上在你没有提交给我们的某些代码的其他地方。

    我在Windows 7上运行VS 2010 Express定位.net 3.5。






    现在您的更新显示你所有的代码,我可以看到发生了什么。 .net代码大概是修剪目录,所以与空白的文件夹丢失。



    因此 @C:\Temp\ 被修剪为 @C:\Temp\



    我发现以下简单的修改避免了无限循环:
    $ b $ pre $ code> TraverseFolders(subFolder + @\);

    添加尾随路径分隔符会停止调用DirectoryInfo时出现的裁剪。在上面的例子中,这意味着 @C:\Temp\\\\会被传递给DirectoryInfo,从而产生预期的结果。



    我想你应该使用一个例程,只添加一个尾随路径分隔符,如果一个不存在。你可能想要避免将@\硬编码为路径分隔符,但是现在你已经知道你知道你的问题的根本原因是什么了。


    My application is C# 3.5 runs on Windows 7 Ultimate, 64 bit. It goes through all folder subfolders to perform its job. However, it fails (falls into the infinite loop until StackOverflow.com exception) if run against the folder which name is only one symbol which is #255.

    To reproduce, you can do the following:

    1. run Windows Explorer create C:\Temp folder in this folder
    2. create new folder and rename it with Alt-255 (using numeric keypad)
    3. create subfolders "first" and "second" there
    4. create subfolders "1" and "2" under Temp

    So you now have:

    • C:\1
    • C:\2
    • C:\ \first
    • C:\ \second

    For such C:\Temp folder with a subfolder with the name #255 (or more #255 symbols) the following code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    class Program
    {
      public static string[] GetDirectories(string pathToTraverse)
      {
        List<string> result = new List<string>();
    
        foreach (DirectoryInfo subFolder in new DirectoryInfo(pathToTraverse).GetDirectories())
        {
          result.Add(subFolder.FullName);
        }
        return result.ToArray();
      }
    
      public static void TraverseFolders(string folderToTraverse)
      {
        foreach (string subFolder in GetDirectories(folderToTraverse))
        {
          Console.WriteLine(subFolder);
    
          TraverseFolders(subFolder);
        }
      }
    
      static void Main(string[] args)
      {
        TraverseFolders(@"C:\Temp");
      }
    }
    

    will never end and will give you result like:

    C:\Temp\ 
    C:\Temp\1
    C:\Temp\2
    C:\Temp\ 
    C:\Temp\1
    C:\Temp\2
    C:\Temp\ 
    C:\Temp\1
    C:\Temp\2
    C:\Temp\ 

    So how do I correctly enumerate such folder subfolders?

    解决方案

    The following program runs perfectly and does not result in a stack overflow error.

    using System;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string pathToTraverse = @"C:\Desktop";
                foreach (DirectoryInfo subFolder in new DirectoryInfo(pathToTraverse).GetDirectories())
                {
                    System.Console.WriteLine(subFolder);
                }
            }
        }
    }
    

    It produces the following output:

    chaff
    Python
    __history
     
    ÿ
    

    The penultimate apparently blank line is in fact the directory named Alt+255.

    Consequently I believe that your problem is not related to the code you have shown and is in fact elsewhere in some code that you have not presented to us.

    I'm running on Windows 7 with VS 2010 Express targeting .net 3.5.


    Now that your update shows all your code, I can see what is happening. The .net code is presumably trimming the directories and so the folders with white space get lost.

    So @"C:\Temp\ " is trimmed to @"C:\Temp\".

    I found the following trivial modification avoided the infinite loop:

    TraverseFolders(subFolder+@"\");
    

    Adding a trailing path separator stops the trimming that appears to occur in the call to DirectoryInfo. In the example above this means that @"C:\Temp\ \" is passed to DirectoryInfo which yields the expected results.

    I guess you should probably use a routine that only adds a trailing path separator if one is not already present. And you may want to avoid hardcoding the @"\" as path separator, but that's for you to work out now that you know what the underlying cause of your problem is.

    这篇关于GetDirectories无法枚举#255名称的文件夹的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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