简体   繁体   English

流程不会以双引号开始

[英]Process won't start with double quotes

I'm trying to copy a folder to a priviledged one (C:\\Program Files (x86)) 我正在尝试将文件夹复制到特权文件夹(C:\\ Program Files(x86))

For that I run a Batch file as administrator through a C# function 为此,我通过C#函数以管理员身份运行批处理文件

Batch file is just: 批处理文件只是:

robocopy %1 %2 /E

When writing manually on windows cmd folder and subs are copied well but it doesn't work in my program, proc.start() closes instantly 当在Windows cmd文件夹上手动写入并且子程序被很好地复制但在我的程序中不起作用时,proc.start()立即关闭

String SourcePath = @"Z:\Path\To\My\Source\Directory";
# Variable contains whitespaces (Program Files (x86))        
String DestinationPath = System.Environment.GetEnvironmentVariable("MYVAR") + "DestDir";

System.Diagnostics.Process proc = new Process();
proc.StartInfo.FileName = @"Z:\Path\To\My\BatFile\File.bat";

# "\"" so there should be no problem with whitespaces
proc.StartInfo.Arguments = SourcePath + " \"" + DestinationPath + "\"";
proc.StartInfo.Verb = "runas";

try
{
    proc.Start();
    proc.WaitForExit();
}
catch (Exception e)
{
    Console.WriteLine("Exception: " + e.Message);
}

Any idea why it doesn't work ? 知道为什么它不起作用吗?

I'm open to smarter idea if you have any 如果您有任何想法,我愿意接受

Thank you 谢谢

When you try to start a .bat file or a .cmd file with rhe runas verb, the system looks up the respective command it should execute in the registry. 当您尝试使用rhe runas动词启动.bat文件或.cmd文件时,系统会查找应在注册表中执行的相应命令。

The registry provides the following command value for the runas verb for .bat and .cmd files: 注册表为.bat.cmd文件的runas动词提供以下命令值:

%SystemRoot%\System32\cmd.exe /C "%1" %*

( %1 would receive the name of the batch file, %* would receive the arguments passed to the batchfile.) %1将接收批处理文件的名称, %*将接收传递给该批处理文件的参数。)

And this command is the cause for the problem. 而此命令就是问题的原因。 What is missing there are some surrounding quotes around "%1" %* (eg, ""%1" %*" ). 缺少的是在"%1" %*周围有一些引号(例如""%1" %*" )。 cmd.exe will be started (which you notice as a briefly appearing cmd shell window). cmd.exe将启动(您会注意到这是一个短暂出现的cmd shell窗口)。 But the way this command provides the arguments to cmd.exe is not correct, causing cmd.exe to fail starting the batch file. 但是此命令向cmd.exe提供参数的方式不正确,从而导致cmd.exe无法启动批处理文件。

Editing this registry value would fix the problem for your program. 编辑此注册表值将解决程序的问题。 However, i do not suggest you modify this registry entry. 但是,我不建议您修改此注册表项。 It could break other programs, and the problem would still exist on any other machine out there that would one day run your program. 它可能会破坏其他程序,并且该问题将仍然存在于有一天将运行您的程序的任何其他计算机上。

Rather, i suggest to start your batch file in conjunction with cmd.exe so you don't rely on the registry's runas command implementation for .bat / .cmd files: 相反,我建议与cmd.exe一起启动您的批处理文件,这样您就不必依赖注册表的.bat / .cmd文件的runas命令实现:

var pathToBatchFile = @"Z:\Path\To\My\BatFile\File.bat";
string SourcePath = ...
string DestinationPath = ...

System.Diagnostics.Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = $"/c \"\"{pathToBatchFile}\" \"{SourcePath}\" \"{DestinationPath}\"\"";
proc.StartInfo.Verb = "runas";
...

(Note that all arguments following "cmd.exe /c" together are framed by a double-quotes pair. Also every single argument itself is put in double-quotes, so this should also work if the source path or the path to the batch file would contain spaces.) (请注意, "cmd.exe /c"之后的所有参数都用双引号对构成。同样,每个参数本身也都用双引号引起来,因此,如果源路径或批处理路径也可以使用,文件将包含空格。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM