通过C#命令提示执行无效 [英] Command prompt execution through C# not working

查看:146
本文介绍了通过C#命令提示执行无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我是C#的初学者,并试图开发一个应用程序......



1.在按钮上单击打开命令提示符

2.转到某个文件夹(复制XYZ.exe的位置)

3.执行一些命令XYZ.exe。



我在google上搜索并编写了一些打开命令提示符但不执行任何操作的代码。我只能看到空白的命令提示符。

这是代码...



Hello,

I am beginner for C# and trying to develop an application that will...

1. open the command prompt on button click
2. Go to certain folder (where an XYZ.exe is copied)
3. execute some commands on the XYZ.exe.

I have searched on google and written some code that opens the command prompt but doesn't execute anything. I can see only blank command prompt.
Here is the code...

private void btn_Merge_Click(object sender, EventArgs e)
        {
            ProcessStartInfo commandInfo = new ProcessStartInfo(); /// ("cmd.exe", "/K");
            commandInfo.WorkingDirectory = @"D:\TOOLS\" ; ///@"c:\";
            commandInfo.UseShellExecute = false;
            commandInfo.RedirectStandardInput = true;
            commandInfo.RedirectStandardOutput = true;
            commandInfo.FileName = "cmd.exe";
            commandInfo.Arguments = "srecord - 1.63 - win32\\srec_cat D:\\FLASH\\BOOT123.hex";
            
            Process process = Process.Start(commandInfo);
            
            command.Close();

        }









请告知我在哪里出错。



我尝试过:


$ b $仅执行commandInfo.Arguments =/ C ipconfig;但即使这样也行不通。



我也尝试过,创建一个Process实例作为......







Please tell me where I am getting wrong.

What I have tried:

ied executing only commandInfo.Arguments = "/C ipconfig"; but even that didn't work.

I also tried, creating an instance of Process as...

Process Command = new Process();

Command.start();
Command.StandardInput.WriteLine("srecord - 1.63 - win32\\srec_cat D:\\FLASH\\BOOT123.hex");
Console.WriteLine(command.StandardOutput.ReadToEnd());

// string result = command.StandardOutput.ReadToEnd();
// Display the command output.
// Console.WriteLine(result);

推荐答案

当你执行cmd.exe它运行命令提示符时,它不会将参数作为命令框的用户输入,它将它们用作cmd指令的参数。

它是参数意味着它根本无法理解您的参数: CMD.exe(命令外壳) - Windows CMD - SS64.com [ ^ ]

你可以这样做 - 可能 - 通过使用/ C或/ K后跟你要执行的命令,但假设 srecord 是你要运行的应用程序,一个bett解决方案是直接执行:

When you execute cmd.exe it runs a command prompt, it doesn't take the parameters as user input to the command box, it uses them as parameters to the cmd instruction.
And it's parameters mean it doesn't understand your arguments at all: CMD.exe (Command Shell) - Windows CMD - SS64.com[^]
You could do this - possibly - by using /C or /K followed by teh command you want to execute, but assuming srecord is the app you want to run, a better solution would be to execute that directly:
commandInfo.FileName = "srecord";
commandInfo.Arguments = "- 1.63 - win32\\srec_cat D:\\FLASH\\BOOT123.hex";

Process process = Process.Start(commandInfo);


我刚试过类似的东西,它运行正常。尝试以下更改,以便您可以实际查看发生的情况:

I just tried something similar and it works fine. Try the following changes so you can actually see what is happening:
commandInfo.RedirectStandardInput = false;
commandInfo.RedirectStandardOutput = false;
commandInfo.FileName = "cmd.exe";
commandInfo.Arguments = "/k srecord - 1.63 - win32\\srec_cat D:\\FLASH\\BOOT123.hex";


伙计们,



这个工作......



Guys,

this worked...

private void ExecuteSrecCommand(String strCommand)
{
    ProcessStartInfo commandInfo = new ProcessStartInfo();
    commandInfo.WorkingDirectory = @"D:\\TOOLS\\srecord-1.63-win32";
    commandInfo.CreateNoWindow = true;
    commandInfo.UseShellExecute = false;
    commandInfo.RedirectStandardInput = false;
    commandInfo.RedirectStandardOutput = false;
    commandInfo.FileName = "cmd.exe";
    commandInfo.Arguments = strCommand;
    Process process = Process.Start(commandInfo);
    process.Close();
}





现在,还有另外一个问题。



上面的代码打开命令提示符并执行命令。

但是,process.Close()不会关闭/删除命令提示符。在任务管理器中,我可以看到很多命令提示符的实例都是打开的。



我试图关闭commandInfo但是commandInfo.Close不存在:(



请告诉我,如何在退出该功能时关闭命令提示符。



Now, there is another problem.

the above code opens the command prompt and executes the command.
But, the process.Close() doesn't closes/deletes the command prompt. In Task manager, I can see many instances of Command prompt are open.

I tried to close the commandInfo but commandInfo.Close doesn't exist :(

Please let me know, how to close the command prompt while exiting the function.


这篇关于通过C#命令提示执行无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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