c#,从可执行文件外部的参数打开特定的 windows 表单

c#, Open a specific windows form from a parameter outside the executable

我是 c# 和 .net 应用程序开发的新手。目前我构建了小型 windows 表单项目。该项目有多个“.cs”形式。我将该项目编译为可执行文件,并使用以下命令从 .net 应用程序调用它。一切正常。

System.Diagnostics.Process.Start("C:\Users\WEI\Desktop\Release\DepartmentManagement.exe");

我有一个新的要求,即当可执行文件为 运行 时动态打开特定表单。我在 "Program.cs" 中硬编码了我想默认打开的表单,如下所示。在下面的示例中,我将其硬编码为默认打开 "ProductionSystem" 表单。我想让它充满活力。

   namespace TestWinform
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new ProductionSystem());
            }
        }
    }

我想在执行应用程序时向可执行文件传递一个值。例如,如果我想默认打开表单 "ProductionDowntime.cs" 而不是 "ProductionSystem",那么我想将 'ProductionDowntime' 作为参数传递,基本上使其成为动态的。 System.Diagnostics.Process.Start("C:\Users\WEIRepService\Desktop\Release\DepartmentDowntime.exe 'ProductionDowntime' "); 我想从可执行文件外部(而不是内部)控制它。请帮忙。提前致谢。

在program.cs中你可以改变

static void Main()

static void Main(string[] args)

然后使用 args 中的任何内容来触发正确的表格。

你会这样称呼你的程序

System.Diagnostics.Process.Start("C:\Users\WEI\Desktop\Release\DepartmentManagement.exe TriggerForm1");

其中 TriggerForm1 基本上是 args

中可用的字符串
using System;
using System.Windows.Forms;

namespace CommandLineParameters_47230955
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            if (args.Length > 0)
            {
                switch (args[0].ToLower())
                {
                    case "triggerform1":
                        Application.Run(new Form1());
                        break;
                    case "triggerform2":
                        Application.Run(new Form2());
                        break;
                    default:
                        break;
                }
            }

        }

    }
}

小菜一碟 using System.Reflection :) 我根据找到的答案做了一个小测试 here Kay Programmer 解释得非常好

基本上,您是通过表单的字符串值从表单的名称调用 from。这样您就可以实用地为其分配您喜欢的任何值。

"query" 部分通过应用我们传递给它的字符串参数来搜索表单的特定名称,因此最后使用结果来为我们初始化表单而不使用它的具体 Class 名称 :)

用它来适应你的例子试试这个:

using System.Reflection;    // add this ;)

namespace TestWinform
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)    // change this ;)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args.Length > 0)       // check here ;)
            {
                var _formName = (from t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                    where t.Name.Equals(args[0])
                    select t.FullName).Single();
                var _form = (Form) Activator.CreateInstance(Type.GetType(_formName));     // get result and cast to Form object in order to run below
                if (_form != null)
                    _form.Show();
            }
            else
            {
                //no argument passed, no form to open ;)
            }


        }
    }
}

就像@blaze_125 在他的回答中提到的那样,如果您需要发送多个表单名称并对此应用功能逻辑,您可以切换/CASE。

例如

static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new Form1());

        if (args.Length > 0)       
        {
            OpenForm(args[0]);
        }

    }

        public static void OpenForm(string FormName)
        {
            var _formName = (from t in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()
                where t.Name.Equals(FormName)
                select t.FullName).Single();
            var _form = (Form)Activator.CreateInstance(Type.GetType(_formName));
            if (_form != null)
                _form.Show();
        }

尝试使用封装表单实例化逻辑的工厂方法

static class Program
{
    static Form CreateFormToOpen(string formName)
    {
        switch (formName)
        {
            case "ProductionDowntime":
                return new ProductionDowntime();

            //  insert here as many case clauses as many different forms you need to open on startup

            default:
                return new ProductionSystem();  //  ProductionSystem is a default form to open
        }
    }

    [STAThread]
    static void Main(string[] args)
    {
        string formName = args.Length > 0 ? args[0] : null;   //  extract form name from command line parameter
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(CreateFormToOpen(formName));    //  use factory method to instantiate form you need to open
    }
}

}