将Windows应用程序作为命令行应用程序运行

时间:2012-11-06 20:00:12

标签: c# .net winforms

我正在开发一个主要是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块时,文本不会在命令提示符处写回用户,这是有问题的,因为我想在命令提示符下更新用户,因为各种执行完成。我可以轻松编写一个独立的控制台应用程序,但我希望提供一个允许给定方案的不同类型的条目的单个工具。我正在寻求做什么,如果是的话,它是如何实现的?

谢谢!

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

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

例如,如果您在UI上接受了“宽度”,“高度”和“深度”,然后计算了音量,则可以将计算放入类库中。

所以你有一个Console应用程序接受三个参数,或者一个带有三个输入的表单应用程序,在这两种情况下,它们都会进行相同的调用......

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

控制台应用程序非常薄,表单应用程序非常精简,因为他们所做的就是获取传递给类库的输入。

答案 2 :(得分:0)

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

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