首页 > 解决方案 > 使用 C# 为 cmd 转义反斜杠

问题描述

我有一个问题,我正在使用 FileBrowserDialog 来获取一些文件夹路由。但这给了我这样的路线: D:\\Angel\\Desktop\\DWServicio

我的问题是:我必须从我的应用程序运行 windows 命令,例如我必须运行这个:dir D:\Angel\Desktop\DWServicio / b / s > .\archivo.txt

但是当我使用 FileBrowserDialog 我的代码行是: dir D:\\Angel\\Desktop\\DWServicio / b / s > .\archivo.txt

所以,我只需要逃避 \\ 并得到 \

在我的代码中:我试图使用替换来逃避,例如:

exe.ExecuteCommandSync(command.Replace(@"\\", @"\"));

或者:

exe.ExecuteCommandSync(command.Replace("\\", @"\"));

等等

但是当我调试结果是一样的: D:\\Angel\\Desktop\\DWServicio

如果我执行这个:

command = @"dir D:\Digital_Solutions / b / s > d:\archivo.txt";
exe.ExecuteCommandSync(command);

完美运行,我的问题是当我尝试使用 FileBrowserDialog 路径时

也许有人可以帮助我吗?提前致谢。

问候

标签: c#cmd

解决方案


您是否尝试过使用 /'s 代替?

快速尝试此代码:

string str = "D:\\Angel\\Desktop\\DWServicio";
Console.WriteLine(str.Replace("\\","/"));

返回 D:/Angel/Desktop/DWServicio

这取决于您的需要可以工作。


推荐阅读