在 C# 中执行命令提示符命令
:)
我有一个可以通过命令行执行的软件,现在我希望它直接从我的 C# 应用程序执行。可悲的是,没有错误,但我仍然做不到。 :(
该软件的.exe文件的路径是C:\program files\mysoftware.exe
我要输入的命令是 cd c:\program files\mysoftware文件夹 进入 mysoftware.exe d:\myfolder\file1.xxx d:\myfolder\file2.xxx -mycommand 进入 上面的命令在实际的命令提示符中运行
得很好,但它们在我的 C# 代码中不起作用。
这是代码:
Process cmdprocess = new Process();
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.FileName = "cmd";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
System.IO.StreamReader sr = cmdprocess.StandardOutput;
System.IO.StreamWriter sw = cmdprocess.StandardInput;
sw.WriteLine(@"echo on");
sw.WriteLine(@"c:");
sw.WriteLine(@"cd" +@"program files\mysoftwarefolder");
sw.WriteLine(@"mysoftware.exe" +@"d:\myfolder\file1.xxx" +@"d:\myfolder\file2.xxx" +@"-mycommand");
sw.WriteLine(@"exit");
sw.Close();
sr.Close();
我猜不正确的部分可能是“startinfo.FileName =“cmd”;”或者我在代码中输入命令的方式,但我不知道如何纠正它们。 :(
请告诉我我做错了什么。我感谢您的每一个回答!:)))
更新感谢您的帮助!我尝试在批处理文件中编写命令,但它仅在调试模式下有效。 (我忘了告诉你们,我正在开发一个 Web 服务。)当我运行将使用此 C# 服务的外部项目时,它将无法工作。我不知道是否应该在代码中添加一些内容。
请帮帮我吧(T___T)
:)
I have a software which can be executed via command line, and now I want it to be executed directly from my C# app. Sadly, there is no error but I still can't do it. :(
The path of .exe file of the software is C:\program files\mysoftware.exe
The command I would like to input is
cd c:\program files\mysoftwareFolder
enter
mysoftware.exe d:\myfolder\file1.xxx d:\myfolder\file2.xxx -mycommand
enter
exit
The commands above work so well in the actual command prompt, but they just don't work from my C# code.
Here is the code:
Process cmdprocess = new Process();
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.FileName = "cmd";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
System.IO.StreamReader sr = cmdprocess.StandardOutput;
System.IO.StreamWriter sw = cmdprocess.StandardInput;
sw.WriteLine(@"echo on");
sw.WriteLine(@"c:");
sw.WriteLine(@"cd" +@"program files\mysoftwarefolder");
sw.WriteLine(@"mysoftware.exe" +@"d:\myfolder\file1.xxx" +@"d:\myfolder\file2.xxx" +@"-mycommand");
sw.WriteLine(@"exit");
sw.Close();
sr.Close();
I guess the incorrect parts might be "startinfo.FileName = "cmd";" or the way I typed the command in the code, but I have no idea how to correct them. :(
Please tell me what I did wrong. I appreciate every answer from you! :)))
UPDATE Thank you for your helps! I tried writing the command in batch file, but it only works in debugging mode. (I forgot to tell you guys that I am developing a web service.) When I run my external project which will use this C# service, it won't work. I don't know whether I should add something to my code or not.
help meeeeee pleaseeeee (T___T)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将这些命令写入批处理文件并执行该批处理文件。
在批处理文件中:
代码:
Write these commands in a batch file and execute the batch file.
In batch file:
Code:
而不是:
直接使用
然后将参数传递给开始信息 因此
整个代码如下所示:
如果您需要查看程序的输出,您可以简单地使用输出字符串。
Instead of:
Directly use
Then pass the arguments to the start info as
So the whole code looks like:
If you need to see output from your program you can simply use the output string.
两件事:我认为您有间距问题,并且您没有阅读这些命令的结果。 cmd 可能会告诉您...“不被识别为内部或外部命令”
如果您查看向 cmd 抛出的内容,它将是:
当您在 cmd 中尝试时,这将不起作用。 CMD 几乎肯定会向您返回错误消息,但您永远不会从 sr 中读取内容,因此您永远不会知道这一点。
我会添加一些空格并将所有路径包含在内部引号中,如下所示:
2 things: I think you have spacing problems and you're not reading the result of these commands. cmd is probably telling you ..."is not recognized as an internal or external command"
If you look at what you're throwing at cmd, it will be:
That won't work when you try it in cmd. CMD is almost certainly kicking back error messages at you, but you're never reading from sr so you'll never know it.
I'd add in some spaces and include all the paths in quotes internally like so: