简体   繁体   English

如何创建也可以在命令提示符下运行的Windows窗体程序

[英]How to create a Windows Forms program that can also run at the command prompt

I've created a program that has two projects: a windows UI and an "engine". 我创建了一个包含两个项目的程序:一个Windows UI和一个“引擎”。 I would like to make it so that the user can execute the program from a command prompt passing in some arguments and the engine will automatically execute them and spit out the results. 我想这样做,以便用户可以从命令提示符执行程序传入一些参数,引擎将自动执行它们并吐出结果。 OR the user can launch the UI and choose all of their options via dropdown lists and such and then click a button to tell the engine to do it's work. 或者,用户可以启动UI并通过下拉列表等选择所有选项,然后单击按钮告诉引擎执行此操作。 I actually already have it working using something I found a long time ago, but I'm trying to see if there's an easier way. 我实际上已经使用了很久以前发现的东西,但是我试图看看是否有更简单的方法。 The way I found involves adding a Win32 class and creating externals to AllocConsole, FreeConsole, and AttachConsole from kernel32.dll. 我发现的方法包括添加一个Win32类并从kernel32.dll创建AllocConsole,FreeConsole和AttachConsole的外部。 Is there an easier way? 有没有更简单的方法? Thanks. 谢谢。

it's simple, make your windows forms application and in the Main method check the command line parameters and in case your parameters are there, instead of calling 它很简单,使你的Windows窗体应用程序和Main方法检查命令行参数,以防你的参数在那里,而不是调用

Application.Run(new Form1());

simply call your engine and start processing. 只需调用您的引擎并开始处理。

it's important to code your engine properly and have also the UI using the same engine when the user is starting those commands from the user interface, so to avoid any useless code duplication. 当用户从用户界面启动这些命令时,正确编码引擎并使用相同引擎的UI也很重要,这样可以避免任何无用的代码重复。

we use this approach in many programs and we are satisfied, if you do not call Application.Run(...) your program will simply terminate when the Main method ends. 我们在许多程序中使用这种方法,我们很满意,如果你不调用Application.Run(...)你的程序将在Main方法结束时终止。

It's even simpler. 它甚至更简单。 Create a Console application. 创建控制台应用程序。 It will parse the command line to get the parameters, then will call your engine. 它将解析命令行以获取参数,然后将调用您的引擎。

Let the WinForms application worry about GUI, let the Console application worry about the command-line. 让WinForms应用程序担心GUI,让Console应用程序担心命令行。

Create four separate configurations, two for GUI and two for Console: 创建四个单独的配置,两个用于GUI,两个用于Console:

DebugConsole,DebugGui,ReleaseConsole,ReleaseGui

The above screenshot is for solution configurations. 以上屏幕截图适用于解决方案配置。 You'll have to do the same again for the project (in the above screenshot the project that has those four configurations is Rummage). 您将不得不为项目再次执行相同的操作(在上面的屏幕截图中,具有这四种配置的项目是Rummage)。 Here is an excerpt from Rummage.csproj : 以下是Rummage.csproj的摘录:

[...]
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugGui|AnyCPU' ">
    <OutputType>WinExe</OutputType>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseGui|AnyCPU' ">
    <OutputType>WinExe</OutputType>
    <Optimize>true</Optimize>
    <DefineConstants>TRACE</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'DebugConsole|AnyCPU'">
    <OutputType>Exe</OutputType>
    <DefineConstants>DEBUG;TRACE;CONSOLE</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'ReleaseConsole|AnyCPU'">
    <OutputType>Exe</OutputType>
    <DefineConstants>TRACE;CONSOLE</DefineConstants>
    <Optimize>true</Optimize>
  </PropertyGroup>
[...]

Notice especially: 特别注意:

  • The Console configurations have <OutputType>Exe</OutputType> (that makes them a console app) while the Release configurations have <OutputType>WinExe</OutputType> . 控制台配置具有<OutputType>Exe</OutputType> (这使它们成为控制台应用程序),而Release配置具有<OutputType>WinExe</OutputType>

  • Both Console configurations have the CONSOLE constant defined. 两种控制台配置都定义了CONSOLE常量。 That way you can use #if CONSOLE [...] #else [...] #endif to have console-/GUI-specific code. 这样你就可以使用#if CONSOLE [...] #else [...] #endif来获得#if CONSOLE [...] #else [...] #endif /特定于GUI的代码。 For example, the Main method might look like this: 例如,Main方法可能如下所示:

     [STAThread] static int Main(string[] args) { #if CONSOLE return MainConsole(args); #else return MainGui(args); #endif } 

    ... and then you can have the WinForms stuff in MainGui and the command-line parsing stuff in MainConsole . ...然后你可以在的WinForms东西MainGui ,并在命令行解析东西MainConsole

This way you can run (and debug) either the GUI or the console version in Visual Studio, while in a build script you can simply build all of them (and into different directories or different EXE file names). 这样,您可以在Visual Studio中运行(和调试)GUI或控制台版本,而在构建脚本中,您可以简单地构建所有这些(以及不同的目录或不同的EXE文件名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建 .bat 文件以使用 VS2012 开发人员命令提示符运行“devenv”命令? - How can i create .bat file to run "devenv" command with VS2012 Developer Command Prompt? C# - 是否可以创建一个可以从带命令行的命令行运行的Windows窗体应用程序? - C# - Is it possible to create a Windows Forms application that can run from the command line with parameters? 我可以在Windows服务中运行另一个命令提示符/ GUI进程吗? - Can I run another Command Prompt/GUI process in a Windows Service? 从命令提示符运行MonoDevlop程序 - Run MonoDevlop program from command prompt 无法在Windows服务中以编程方式运行命令提示符 - Unable to run command prompt programmatically in Windows service 如何在不考虑是否在C#Windows窗体中传递参数的情况下创建动态SQL查询以运行 - How can we create a dynamic sql query to run without considering if the parameters are passed or not in c# windows forms 通过命令提示符运行表单应用程序并向其发送命令 - Run forms Application through command prompt and send commands to it 如何用命令运行程序? - how to run program with command? Windows 窗体中的提示对话框 - Prompt Dialog in Windows Forms 执行程序并通过 windows 命令提示符输入 'string[] args' - Execute program and enter 'string[] args' through windows command prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM