C# 如何在 process.arguements 中使用目录空格?

发布于 2024-10-07 13:03:24 字数 881 浏览 0 评论 0原文

创建的程序利用第三方工具生成日志文件。

但是,为该工具提供的参数需要目录位置中的各种文件作为生成日志的一部分。因此,将使用 @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + RestoreFolder.Name + " -p runmru"; 的主要参数生成日志。

有人可以建议如何使系统处理“C:\System Volume Information\”的参数并放置空格吗?谢谢!

代码:

            Process process = new Process();
            process.StartInfo.FileName = @"C:\test\ftk\ripxp\ripxp.exe";
            process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();

The program created utilizes a 3rd party tool to generate a log file.

However the arguments provided for the the tool requires various files from Directory locations as part of generating the logs. Therefore the main argument of @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru"; would be used to generate the logs.

May someone advise on how to make the arguments of "C:\System Volume Information\" be processed by the system with the white spaces in placed? Thanks!

The codes:

            Process process = new Process();
            process.StartInfo.FileName = @"C:\test\ftk\ripxp\ripxp.exe";
            process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

[旋木] 2024-10-14 13:03:25

您需要通过向其附加 \ (\") 来转义 " - 对于普通字符串,或者将它们加倍 (" ") 用于逐字字符串文字(以 @ 开头的字符串):

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d ""C:\System Volume Information\" + restoreFolder.Name + @""" -p runmru";

You need to escape the " by appending a \ to them (\") - for normal strings, or doubling them ("") for verbatim string literals (those starting with @):

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d ""C:\System Volume Information\" + restoreFolder.Name + @""" -p runmru";
痴情换悲伤 2024-10-14 13:03:25

将该路径用双引号引起来:

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d ""C:\System Volume Information\" + restoreFolder.Name + @""" -p runmru";

Wrap that path in double quotes:

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d ""C:\System Volume Information\" + restoreFolder.Name + @""" -p runmru";
九歌凝 2024-10-14 13:03:25

也许

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";

应该是

process.StartInfo.Arguments = @"-r ""C:\test\ftk\ntuser.dat"" -d ""C:\System Volume Information\""" + restoreFolder.Name + " -p runmru";

Perhaps

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";

should be

process.StartInfo.Arguments = @"-r ""C:\test\ftk\ntuser.dat"" -d ""C:\System Volume Information\""" + restoreFolder.Name + " -p runmru";
莫多说 2024-10-14 13:03:25

如果我正确理解了这个问题,您可以用引号将名称括起来:

"... \"C:\System Volume Information\" + restoreFolder.Name + "\"..."

If I correctly understood the question, you can wrap the name with quotes:

"... \"C:\System Volume Information\" + restoreFolder.Name + "\"..."
尤怨 2024-10-14 13:03:25

您确实需要使用 string.Format 和 Path 类:

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru"

可以重写为更清晰,如下所示:

string ntuser = @"C:\test\ftk\ntuser.dat";
var args = Path.Combine(@"C:\System Volume Information\", "restoreFolder.Name");

var outs = string.Format("-r {0} -d {1} -p runmru", ntuser, args);
outs.Dump();

You really need to use a string.Format along with the Path class:

process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru"

can be rewritten to be a lot cleaner as follows:

string ntuser = @"C:\test\ftk\ntuser.dat";
var args = Path.Combine(@"C:\System Volume Information\", "restoreFolder.Name");

var outs = string.Format("-r {0} -d {1} -p runmru", ntuser, args);
outs.Dump();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文