在下面的代码中,我有两个变量,一个是currentDirectoryPath,另一个是rootPath。我希望使用Directory.GetDirectories()函数枚举这两个根路径中的所有子文件夹。但是,当我通过currentDirectoryPath时,代码运行良好,但是不是 rootPath.,而是抛出了异常
NotSupportedException:不支持给定路径的格式。
我已经用以下两条路径测试了代码:
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()应该使用提供的路径,并且它应该枚举所有的子文件夹。
发布于 2019-06-06 07:42:16
你有一个不正确的rootPath,只要看看潮湿
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}")));结果:
\u202a :
\u0043 : C
\u003a : :
\u005c : \
\u0055 : U
\u0073 : s
\u0065 : e
\u0072 : r
\u0073 : s
... 请注意\u202a字符代码(左向右嵌入),它不能出现在有效路径中。您所要做的就是重新键入"C:片段,以消除不可见的从左到右的嵌入符号。
https://stackoverflow.com/questions/56472909
复制相似问题