我写这个程序是为了在Windows命令提示符下用C#应用程序打开记事本,但它不能
问题出在哪里?
namespace msdos
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
p.WaitForExit();
using (StreamWriter sw = p.StandardInput)
if (sw.BaseStream.CanWrite) { sw.WriteLine("notepad.exe"); }
}
}
}
尝试启动任何类似的命令:
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "notepad.exe");
您可以用任何命令替换notepad.exe。
试试这个:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C notepad.exe";
process.StartInfo = startInfo;
process.Start();
或者更简单:
string strCmdText;
strCmdText = "/C notepad.exe";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);