在Windows命令提示符下使用C#应用程序打开记事本

时间:2015-06-30 23:25:43

标签: c# windows cmd notepad system.diagnostics

我编写了这个程序,在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"); }
    }
}
}

2 个答案:

答案 0 :(得分:4)

尝试启动任何类似的命令:

ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "notepad.exe");

您可以通过任何命令替换notepad.exe。

答案 1 :(得分:1)

试试这个:

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);