Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何读取文件夹名称中包含方括号的文件路径_C#_Filepath_Brackets_Square - Fatal编程技术网

C# 如何读取文件夹名称中包含方括号的文件路径

C# 如何读取文件夹名称中包含方括号的文件路径,c#,filepath,brackets,square,C#,Filepath,Brackets,Square,我有一个文件路径,如下所示: C:\Recordings\Public\20160901\[MYPC] 我无法更改文件夹的格式 我正在尝试使用以下代码读取文件夹的内容: foreach (string file in Directory.EnumerateFiles(args[0], "*.*")) { string contents = File.ReadAllText(file); Console.WriteLine(contents); } 我收到以下错误: Unhan

我有一个文件路径,如下所示:

C:\Recordings\Public\20160901\[MYPC]
我无法更改文件夹的格式

我正在尝试使用以下代码读取文件夹的内容:

foreach (string file in Directory.EnumerateFiles(args[0], "*.*"))
{
    string contents = File.ReadAllText(file);
    Console.WriteLine(contents);
}
我收到以下错误:

Unhandled Exception: System.NotSupportedException: The given path's format is not supported.
我知道,因为我以前在powershell中使用过这种方法,并且取得了很好的效果,所以它似乎不适用于c。我正在通过命令行args[0](控制台应用程序)设置读取路径

欢迎提出任何想法/建议

更新

我调用我的应用程序时出现以下错误:

myapp.exe "c:\My Recordings\Public\20160905\[CDPC]\"
以下情况没有:

myapp.exe "c:\My Recordings\Public\20160905\[CDPC]"

只要删除包装引号(如果存在)

foreach (string file in Directory.EnumerateFiles(args[0].Replace("\"",""), "*.*"))
            {
                string contents = File.ReadAllText(file);
                Console.WriteLine(contents);
            }
它也可以(更精确地)用正则表达式来完成

Regex.Replace(args[0], "(^\")|(\"$)", "") //instead of args[0].Replace("\"","")

一个难看的解决方法是在每个坏字符的实例前面加上“\\”(这只是一个字符,转义)无法复制您的问题:这对我来说很有效,您不需要任何特殊技巧,只需在命令行中键入路径作为参数。嗯……它不应该是args[1]吗?args[0]通常包含可执行文件name@Pikoh不,是args[0]。。。再一次,我正在测试代码,它正在工作indeed@MachineLearning这可能是因为在实际目录名(即:myapp.exe“C:\My Recordings\Public\20160901[MYPC]\)中有空格)时,它被封装为双引号