首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Directory.GetDirectories抛出异常

Directory.GetDirectories抛出异常
EN

Stack Overflow用户
提问于 2019-06-06 07:37:59
回答 1查看 193关注 0票数 2

在下面的代码中,我有两个变量,一个是currentDirectoryPath,另一个是rootPath。我希望使用Directory.GetDirectories()函数枚举这两个根路径中的所有子文件夹。但是,当我通过currentDirectoryPath时,代码运行良好,但是不是 rootPath.,而是抛出了异常

NotSupportedException:不支持给定路径的格式。

我已经用以下两条路径测试了代码:

代码语言:javascript
复制
 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

   namespace ConsoleApp
   {
       class Program
       {
           static void Main(string[] args)
           {
               string currentDirectoryPath = Directory.GetCurrentDirectory();
               string rootPath = "‪C:\\Users\\Ravi.Reddy\\Desktop\\Practise";

               string[] dirs1 = Directory.GetDirectories(
                   currentDirectoryPath, 
                  "*.*", 
                   SearchOption.AllDirectories);

              foreach (var dir in dirs1)
                  Console.WriteLine(dir);

              string[] dirs2 = Directory.GetDirectories( // <- Exception here
                  rootPath, 
                 "*.*",
                  SearchOption.AllDirectories);

              foreach (var dir in dirs2)
                  Console.WriteLine(dir);

              Console.ReadLine();
           }
      }
 }

我希望Directory.GetDirectories()应该使用提供的路径,并且它应该枚举所有的子文件夹。

EN

回答 1

Stack Overflow用户

发布于 2019-06-06 07:42:16

你有一个不正确的rootPath,只要看看潮湿

代码语言:javascript
复制
  using System.Linq;

  ...

  // Copy + Paste from the question
  string rootPath = "‪C:\\Users\\Ravi.Reddy\\Desktop\\Practise";

  Console.Write(string.Join(Environment.NewLine, 
    rootPath.Select(c => $"\\u{(int)c:x4} : {c}")));

结果:

代码语言:javascript
复制
\u202a : ‪
\u0043 : C
\u003a : :
\u005c : \
\u0055 : U
\u0073 : s
\u0065 : e
\u0072 : r
\u0073 : s
 ... 

请注意\u202a字符代码(左向右嵌入),它不能出现在有效路径中。您所要做的就是重新键入"‪C:片段,以消除不可见的从左到右的嵌入符号。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56472909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档