简体   繁体   English

如何从C#执行命令?

[英]How to execute a command from C#?

I am trying to execute a code from c# windows form application. 我正在尝试从C#Windows窗体应用程序执行代码。

 string cmdCode = "/C mogrify -crop 590x389+116+102! D:\\Backup\\Images\\*.TIF";

 System.Diagnostics.Process.Start("CMD.exe", cmdCode);

But It doesnt do what it is supposed to do? 但是它没有执行应做的事情?

the code perfectly does the job when I type it in the command line. 当我在命令行中键入代码时,代码就可以完美地完成工作。 I already tried to change the path. 我已经尝试过更改路径。 I placed the tif files with MyApp.exe file and changed the cmdCode as 我将tif文件与MyApp.exe文件放置在一起,并将cmdCode更改为

cmdCode = "/C mogrify -crop 590x389+116+102! *.TIF"; 

no success.. It shows up the black command promt very quickly and it gets disappeared. 没有成功..它很快显示了黑色命令提示符,并且消失了。

I also tried to put the code in a file and make the extension .bat to run it but still no success!! 我还尝试将代码放入文件中,并使其扩展名为.bat来运行它,但仍然没有成功!! any suggestions ? 有什么建议么 ?

Thanks 谢谢

Remove the cmd.exe part and instead place there the path to mogrify.exe and start the parameters with "-crop...", removing the /C 删除cmd.exe部分,然后在其中放置mogrify.exe的路径,并使用“ -crop ...”启动参数,删除/ C

If you're passing along path names that contain spaces you'll need to enclose the path with quotes, so the parameters will look like this: 如果要传递包含空格的路径名,则需要用引号将路径引起来,因此参数将如下所示:

"-crop \\"590x389+116+102!\\" \\""+YourPathHere+"\\""; “ -crop \\” 590x389 + 116 + 102!\\“ \\”“ + YourPathHere +” \\“”;

Try replacing the /C option with /K. 尝试用/ K替换/ C选项。 That will not solve your problem, but it should prevent the command prompt from disappearing, and allow you to see if any error is displayed by the prompt. 那不会解决您的问题,但是应该防止命令提示符消失,并允许您查看提示符是否显示任何错误。

It's also possible that the PATH you are passing to CMD.exe is different from the one used by default, for some reason. 由于某种原因,传递给CMD.exe的PATH也有可能与默认使用的PATH不同。 Once you've started cmd.exe with the /K option, you should also be able to issue a echo %PATH% command, and see what you've got. 使用/ K选项启动cmd.exe之后,您还应该能够发出echo%PATH%命令,并查看所获得的内容。

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

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