如何以编程方式在cmd.exe上运行控制台应用程序? [英] How to run console application on cmd.exe programatically?
问题描述
我创建了一个控制台应用程序,当我运行项目控制台时,application.exe运行并显示命令提示符,我要做的就是在此提示符(exe)上正常工作但我想在其上运行它(cmd.exe )从哪里我必须接收输入所以我的问题是如何在cmd.exe上运行控制台应用程序?
命名空间 ConsoleApplication2
{
< span class =code-keyword> class 计划
{
public static void Main( string [] args)
{
Program p = new Program();
p.getConsoleInput(); // 从提示中提取字符串
p。 ReadWriteIntoFile(); // 生成txt文件并将字符串转储到其中//
p。 WriteFileOutput(); // 这会根据字符串将相关数据放入另一个文本文件中.//
}
}
}
如果你想调用你的exe形式一个控制台你必须使用'cmd'中的一个开关/ c或/ K执行切换后的命令:
string strCmdPars;
strCmdPars = / C application.exe;
System.Diagnostics.Process.Start( CMD.exe,strCmdPars);
这将运行cmd.exe,而cmd.exe将运行您的应用程序。当应用程序结束时,cmd将退出。
使用开关/ K cmd将在application.exe退出后保持打开状态:
string strCmdPars;
strCmdPars = / K application.exe;
System.Diagnostics.Process.Start( CMD.exe,strCmdPars);
I have created one console application and when i run project console application.exe runs and shows command prompt , and what i have to do is works fine on this prompt(exe) but i want to run it on (cmd.exe) from where i have to receive input so my question is "How to run console application on cmd.exe?"
namespace ConsoleApplication2
{
class Program
{
public static void Main(string[] args)
{
Program p = new Program();
p.getConsoleInput();// TAKING STRING FROM PROMPT
p.ReadWriteIntoFile();//generate txt file and dump string into it//
p.WriteFileOutput();// this put related data according to string into another text file.//
}
}
}
If you want invoke your exe form a console you have to use one of the switches /c or /K of 'cmd' that executes the command following the switch:
string strCmdPars; strCmdPars= "/C application.exe"; System.Diagnostics.Process.Start("CMD.exe",strCmdPars);
This will run cmd.exe that in turn will run your application. When the application ends cmd will quit.
Using the switch /K cmd will remain on after application.exe quits:
string strCmdPars; strCmdPars= "/K application.exe"; System.Diagnostics.Process.Start("CMD.exe",strCmdPars);
这篇关于如何以编程方式在cmd.exe上运行控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!