2

I am working on a migration tool that is primarily a Windows Forms application. What I want to do is provide the ability to run the application as a sort of command line utility where parameters can be passed in and the migration occurs completely void of the GUI. This seems straight forward enough and the entry point for my application looks like this:

    [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();
        }
    }

The problem I am running into is that when execution passes into the else block the text is not wrote back to the user at the command prompt which is problematic as I want to update the user at the command prompt as various executions complete. I could easily write a standalone console application but I was hoping to provide a single tool that allowed to different types of entries for a given scenario. Is what I am looking to do possible and, if so, how is it achieved?

Thanks!

4

3 回答 3

3

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

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

于 2012-11-06T20:05:17.920 回答
2

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

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

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

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

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

于 2012-11-06T20:06:12.147 回答
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;
    }
}
于 2014-01-14T03:29:55.403 回答