【问题标题】:How to get list of all valid folder and file characters for Windows file system paths in .Net如何在.Net中获取Windows文件系统路径的所有有效文件夹和文件字符列表
【发布时间】:2012-11-12 18:49:49
【问题描述】:

System.IO.Path.GetInvalidPathChars() 给出无效字符列表。

但我需要列出所有有效字符。

想到的第一个简单的想法是从 0 迭代到 255 并排除无效字符,但它会只给出有效字符吗?还有 Unicode 呢?我应该从 0 迭代到 65535 吗?

【问题讨论】:

    标签: .net windows file-io character


    【解决方案1】:

    AFAIK .NET 中没有任何东西可以获取这些信息。

    您可以查看以下微软页面:http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx

    相关摘录:

    Use any character in the current code page for a name, including Unicode characters and characters
    in the extended character set (128–255), except for the following:
    
        The following reserved characters:
    
            < (less than)  
            > (greater than)
            : (colon)
            " (double quote)
            / (forward slash)
            \ (backslash)
            | (vertical bar or pipe)
            ? (question mark)
            * (asterisk)
    
        Integer value zero, sometimes referred to as the ASCII NUL character.
    
        Characters whose integer representations are in the range from 1 through 31,
        except for alternate data streams where these characters are allowed.
    
        Any other character that the target file system does not allow.
    

    【讨论】:

      【解决方案2】:

      这个怎么样:

          private static IEnumerable<char> GetValidFileNameChars()
          {
              var invalidChars = new List<char>(System.IO.Path.GetInvalidFileNameChars());
              var allChars = new List<char>();
              for (var i = 0; i < 255; i++)
              {
                  allChars.Add((char) i);
              }
      
              var validChars = allChars.AsEnumerable().Except(invalidChars.AsEnumerable());
              return validChars;
          }
      

      或者,您可以从 0 迭代到 65535(char.MinValuechar.MaxValue)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2018-03-02
        • 2013-08-05
        • 1970-01-01
        • 1970-01-01
        • 2013-11-26
        相关资源
        最近更新 更多