如何在C#中使用更多DOS命令

发布于 2024-11-28 17:04:43 字数 469 浏览 2 评论 0原文

我在 DOS 中有大约 7 个命令,我想在我的 C# 程序中运行它们。我可以这样做吗:

System.Diagnostics.Process.Start("cmd.exe", "my more commands here");

编辑: 我正在制作一个可以运行 g++ 的小应用程序。现在这是正确的吗?:

 System.Diagnostics.Process.Start("cmd.exe", "/k cd C:\\Alps\\compiler\\ /k g++ C:\\Alps\\" + project_name + "\\Debug\\Main.cpp");

编译命令:

g++ -c C:\Alps\here_is_projectname\Debug\Main.cpp -o main.o

I have about 7 commands in DOS and I want to run them in my C# program. Can I do:

System.Diagnostics.Process.Start("cmd.exe", "my more commands here");

?
EDIT:
I'm making small app what will run g++. Is this now correct?:

 System.Diagnostics.Process.Start("cmd.exe", "/k cd C:\\Alps\\compiler\\ /k g++ C:\\Alps\\" + project_name + "\\Debug\\Main.cpp");

Command for compiling:

g++ -c C:\Alps\here_is_projectname\Debug\Main.cpp -o main.o

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清眉祭 2024-12-05 17:04:43
cmd.exe /k <command>
cmd.exe /c <command>

两者均有效。

  • /k 将执行命令并留下一个空提示(如果您只想执行以获得反馈,则在您的应用程序中可能不太理想。)
  • /c 将执行命令并在完成后关闭窗口。

如果您想从特定目录执行命令,您可以这样做:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
p.StartInfo.Arguments = String.Format(@"/c g++ ""C:\Alps\{0}\Debug\Main.cpp""", project_name);
p.StartInfo.WorkingDirectory = @"C:\Alps\compiler";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.ErrorDialog = false;
p.Start();
cmd.exe /k <command>
cmd.exe /c <command>

Are both valid.

  • /k will execute the command and leave you with an empty prompt (probably less desirable in your application if you just want to execute for feedback.)
  • /c will execute the command and close the window when it has completed.

If you're looking to execute a command from a specific directory, you can do:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
p.StartInfo.Arguments = String.Format(@"/c g++ ""C:\Alps\{0}\Debug\Main.cpp""", project_name);
p.StartInfo.WorkingDirectory = @"C:\Alps\compiler";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.ErrorDialog = false;
p.Start();
悲念泪 2024-12-05 17:04:43

是的,您可以使用“/C”开关传入命令行:

System.Diagnostics.Process.Start("cmd.exe", "/C dir");

Yes, you can pass in the command line using the "/C" switch:

System.Diagnostics.Process.Start("cmd.exe", "/C dir");
小…红帽 2024-12-05 17:04:43

您还可以执行以下操作...

Process.Start(new ProcessStartInfo()
{
 Arguments = "args",
 WorkingDirectory = "C:\SomePath",
 UseShellExecute= true,
 FileName = ".exe"
});

如果您需要,processstartinfo 上还有一些选项可以重定向输入和输出

例如..

Process.Start(new ProcessStartInfo()
{
 Arguments = "C:\\Alps\\" + project_name + "\\Debug\\Main.cpp",
 WorkingDirectory = "C:\\Apls\\",
 UseShellExecute= true,
 FileName = "g++.exe"
});

You can also do like the following....

Process.Start(new ProcessStartInfo()
{
 Arguments = "args",
 WorkingDirectory = "C:\SomePath",
 UseShellExecute= true,
 FileName = ".exe"
});

There are also options on the processstartinfo to redirect input and output if you need to

For example..

Process.Start(new ProcessStartInfo()
{
 Arguments = "C:\\Alps\\" + project_name + "\\Debug\\Main.cpp",
 WorkingDirectory = "C:\\Apls\\",
 UseShellExecute= true,
 FileName = "g++.exe"
});
葬﹪忆之殇 2024-12-05 17:04:43

您可以启动 cmd.exe 重定向与您的命令一起传输的标准输入和脚。

 process.Start(...);


                    process.StandardInput.WriteLine("Dir xxxxx");
                    process.StandardInput.WriteLine("Dir yyyyy");
                    process.StandardInput.WriteLine("Dir zzzzzz");
                    process.StandardInput.WriteLine("other command(s)");

当然,您应该记住设置进程星号信息以表明您想要重定向输入:

 ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe);
        processStartInfo.CreateNoWindow = true;

        processStartInfo.ErrorDialog = false;

        processStartInfo.RedirectStandardInput = true;

You can launch cmd.exe redirect the stdin and feet that stream with your commands.

 process.Start(...);


                    process.StandardInput.WriteLine("Dir xxxxx");
                    process.StandardInput.WriteLine("Dir yyyyy");
                    process.StandardInput.WriteLine("Dir zzzzzz");
                    process.StandardInput.WriteLine("other command(s)");

Of course you should remeber to set your process star info to say you want redirect input:

 ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe);
        processStartInfo.CreateNoWindow = true;

        processStartInfo.ErrorDialog = false;

        processStartInfo.RedirectStandardInput = true;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文