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

C# 将Windows应用程序作为命令行应用程序运行,c#,.net,winforms,C#,.net,Winforms,我正在开发一个迁移工具,它主要是一个Windows窗体应用程序。我想做的是提供将应用程序作为一种命令行实用程序运行的能力,其中可以传递参数,并且迁移完全在GUI中进行。这看起来很简单,我的应用程序的入口点如下所示: [STAThread] static void Main(string[] args) { if (args.Length == 0) { Application.EnableVisualStyles()

我正在开发一个迁移工具,它主要是一个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块时,文本不会在命令提示符下写回用户,这是有问题的,因为我希望在各种执行完成时在命令提示符下更新用户。我可以很容易地编写一个独立的控制台应用程序,但我希望提供一个工具,允许为给定场景编写不同类型的条目。我希望做的事情可能吗?如果可能,是如何实现的


谢谢

以下是一个讨论此问题的主题:


键是从kernel32.dll调用函数这里有一个主题讨论这个问题:


关键是从kernel32.dll调用函数,通常的模式是将逻辑写入一个类库,您可以从可视化UI或命令行应用程序调用该类库

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

所以你要么有一个接受三个参数的控制台应用程序,要么有一个有三个输入的表单应用程序,在这两种情况下,它们进行相同的调用

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

console应用程序非常精简,forms应用程序非常精简,因为它们所做的只是获取要传递到类库的输入。

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

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

所以你要么有一个接受三个参数的控制台应用程序,要么有一个有三个输入的表单应用程序,在这两种情况下,它们进行相同的调用

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

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

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

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;
    }
}
名称空间测试
{
部分类RunnableForm
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.button1=new System.Windows.Forms.Button();
这个.SuspendLayout();
// 
//按钮1
// 
this.button1.Location=新系统图纸点(42,42);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(153,66);
this.button1.TabIndex=0;
this.button1.Text=“button1”;
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=新系统.EventHandler(this.button1\u Click);
// 
//流形
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(284261);
this.Controls.Add(this.button1);
this.Name=“RunnableForm”;
this.Text=“RunnableForm”;
此选项为.resume布局(false);
}
#端区
private System.Windows.Forms.Button按钮1;
}
}

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

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;
    }
}
名称空间测试
{
部分类RunnableForm
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.button1=new System.Windows.Forms.Button();
这个.SuspendLayout();
// 
//按钮1
// 
this.button1.Location=新系统图纸点(42,42);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(153,66);
this.button1.TabIndex=0;
this.button1.Text=“button1”;
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=新系统.EventHandler(this.button1\u Click);
// 
//流形
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=Sy