【问题标题】:Running a Windows application as a Command Line application将 Windows 应用程序作为命令行应用程序运行
【发布时间】:2012-10-26 20:15:18
【问题描述】:

我正在开发一个主要是 Windows 窗体应用程序的迁移工具。我想要做的是提供将应用程序作为一种命令行实用程序运行的能力,可以在其中传递参数并且完全没有 GUI 的情况下发生迁移。这看起来很简单,我的应用程序的入口点如下所示:

    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainWindow());
        }
        else
        {
            //Command Line Mode
            Console.WriteLine("In Command Line Mode");
            Console.ReadLine();
        }
    }

我遇到的问题是,当执行进入 else 块时,文本不会在命令提示符处写回用户,这是有问题的,因为我想在各种执行完成时在命令提示符处更新用户。我可以很容易地编写一个独立的控制台应用程序,但我希望提供一个单一的工具来允许给定场景的不同类型的条目。我想要做的事情是否可行,如果可以,它是如何实现的?

谢谢!

【问题讨论】:

标签: c# .net winforms


【解决方案1】:

这里有一个讨论这个的话题:http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/thread/875952fc-cd2c-4e74-9cf2-d38910bde613/

关键是从kernel32.dll调用AllocConsole函数

【讨论】:

    【解决方案2】:

    通常的模式是将逻辑写入类库,您可以从可视化 UI 或命令行应用程序调用该类库。

    例如,如果您在 UI 上接受“宽度”、“高度”和“深度”,然后计算体积,您会将计算结果放入类库中。

    因此,您要么有一个接受三个参数的控制台应用程序,要么有一个具有三个输入的表单应用程序,在这两种情况下,它们都会进行相同的调用...

    var volumeCalculations = new VolumeCalculations();
    var volume = volumeCalculations.GetVolume(width, height, depth);
    

    控制台应用程序非常精简,表单应用程序也非常精简,因为它们所做的只是将输入传递给类库。

    【讨论】:

      【解决方案3】:

      这里是完整的可运行示例。编译:

      csc RunnableForm.cs RunnableForm.Designer.cs
      

      RunnableForm.cs:

      using System;
      using System.Linq;
      using System.Windows.Forms;
      
      namespace Test
      {
          public partial class RunnableForm : Form
          {
              public RunnableForm()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  MessageBox.Show("bang!");
              }
      
              [STAThread]
              static void Main()
              {
      
                  string[] args = Environment.GetCommandLineArgs();
                  // We'll always have one argument (the program's exe is args[0])
                  if (args.Length == 1)
                  {
                      // Run windows forms app
                      Application.Run(new RunnableForm());
                  }
                  else
                  {
                      Console.WriteLine("We'll run as a console app now");
                      Console.WriteLine("Arguments: {0}", String.Join(",", args.Skip(1)));
                      Console.Write("Enter a string: ");
                      string str = Console.ReadLine();
                      Console.WriteLine("You entered: {0}", str);
                      Console.WriteLine("Bye.");
                  }
              }
          }
      }
      

      RunnableForm.Designer.cs:

      namespace Test
      {
          partial class RunnableForm
          {
              /// <summary>
              /// Required designer variable.
              /// </summary>
              private System.ComponentModel.IContainer components = null;
      
              /// <summary>
              /// Clean up any resources being used.
              /// </summary>
              /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
              protected override void Dispose(bool disposing)
              {
                  if (disposing && (components != null))
                  {
                      components.Dispose();
                  }
                  base.Dispose(disposing);
              }
      
              #region Windows Form Designer generated code
      
              /// <summary>
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// </summary>
              private void InitializeComponent()
              {
                  this.button1 = new System.Windows.Forms.Button();
                  this.SuspendLayout();
                  // 
                  // button1
                  // 
                  this.button1.Location = new System.Drawing.Point(42, 42);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(153, 66);
                  this.button1.TabIndex = 0;
                  this.button1.Text = "button1";
                  this.button1.UseVisualStyleBackColor = true;
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  // 
                  // RunnableForm
                  // 
                  this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                  this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                  this.ClientSize = new System.Drawing.Size(284, 261);
                  this.Controls.Add(this.button1);
                  this.Name = "RunnableForm";
                  this.Text = "RunnableForm";
                  this.ResumeLayout(false);
      
              }
      
              #endregion
      
              private System.Windows.Forms.Button button1;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-18
        相关资源
        最近更新 更多