在的Process.Start参数传递报价 [英] Passing quotes in Process.Start arguments

查看:153
本文介绍了在的Process.Start参数传递报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET中我跑这条线

  VAR P =的Process.Start(@CMD,@/ C MKLINK / H C:\ Z \ BC:\ Z \ A \);
 

本工程所有罚款,但是我很担心,如果两个args作为MKLINK的人有一个空间,这不会正常工作。所以我说围绕这两个参数。执行该行不再工作,当我写了\相反,它仍然没有奏效。

我怎样写,当我执行报价 CMD / C

解决方案

 字符串SOURCEPATH = @C:\ Z \ B;
字符串TARGETPATH​​ = @C:\ Z \一;

字符串参数=的String.Format(\{0} \\{1} \,SOURCEPATH,TARGETPATH​​);

变种p值=的Process.Start(CMD,/ C MKLINK / H+的参数);
 

工作的例子:

 字符串SOURCEPATH = @C:\ Documents和Settings \哈维罗伯特\我的文档\ Test.txt的;
字符串TARGETPATH​​ = @C:\测试;

字符串s =的String.Format(\{0} \\{1} \,SOURCEPATH,TARGETPATH​​);
的Process.Start(CMD,@/ C复制+ S);
 

1文件复制。

In .NET i'm running this line

var p = Process.Start(@"cmd", @"/C mklink /H c:\z\b c:\z\a\");

This works all fine however I'm worried that if one of the two args for mklink has a space that this wont work correctly. So I added "" around both parameters. Executing this line no longer worked and when I wrote \"" instead it still didn't work.

How do I write quotes when I am executing cmd /C?

解决方案

string sourcePath = @"c:\z\b";
string targetPath = @"c:\z\a";

string arguments = string.Format("\"{0}\" \"{1}\"", sourcePath, targetPath);

var p = Process.Start("cmd", "/C mklink /H " + arguments);

Working example:

string sourcePath = @"c:\documents and settings\harvey robert\My Documents\Test.txt";
string targetPath = @"c:\test";

string s = string.Format("\"{0}\" \"{1}\"", sourcePath, targetPath);
Process.Start("cmd", @"/c copy " + s);

1 files copied.

这篇关于在的Process.Start参数传递报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆