C# Process.StartInfo.FileName为空
因此,我正在创建一个C#程序,在按下按钮时运行命令提示符命令,这是按下按钮时执行的代码:C# Process.StartInfo.FileName为空,c#,C#,因此,我正在创建一个C#程序,在按下按钮时运行命令提示符命令,这是按下按钮时执行的代码: private void button1_Click(object sender, EventArgs e) { System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostic
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
string arg = textBox1.Text + "& exit";
process.StartInfo.Arguments = arg;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo = startInfo;
process.Start();
string outp = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
StreamWriter myStreamWriter = process.StandardInput;
myStreamWriter.WriteLine(textBox1.Text);
textBox2.Text = outp;
process.WaitForExit();
}
也许我遗漏了一些明显的东西,如果是的话,我很抱歉(我是C#的初学者),但我不能,就我的一生而言,弄清楚为什么我会从system.dll中抛出一个异常,其内容如下:
System.dll中发生类型为“System.InvalidOperationException”的未处理异常
其他信息:无法启动进程,因为尚未提供文件名
但是,我提供了一个文件名(代码段的第5行)。非常感谢您的帮助,谢谢。
附笔。
该代码使用:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
要正常运行。是否需要在上面显示的文件名中添加转义字符?Ie“c:\\windows\\system32\\cmd.exe”或“c:\windows\system32\cmd.exe”?我做了@“c”\…,cmd.exe的路径是正常的,如果您添加“System.Diagnostics.Process Process=new System.Diagnostics.Process(),则无法更改;“我很抱歉之前评论了一条评论,要求人们投票支持这个问题,我只是希望有一个更高的代表能够发表评论,并公开投票支持其他人的问题。我还认为这是一个可以问的问题,没有意识到我这样做会失去代表。
process.StartInfo = startInfo; //Here problem is there, you are
//refreshing "process.StartInfo" with "startInfo". As "startInfo" is empty in your code.
//So use below code
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
string arg = textBox1.Text + "& exit";
startInfo.Arguments = arg;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "cmd.exe";
process.StartInfo = startInfo;
process.Start();