C#:我想像文件路径一样将消息传递给我的表单应用程序,比如控制台应用程序,我该怎么做?

7 c# command-line arguments winforms

C#:我想像文件路径一样将消息传递给我的表单应用程序,比如控制台应用程序,我该怎么做?

我被告知我需要找到我的主要方法来添加string [] args,但我不知道哪个是Windows窗体.我的主要方法是哪种C#windows窗体应用程序?

Aut*_*act 8

好的,string [] args = Environment.GetCommandLineArgs()是一个更好的选择.但我会保留以下答案作为替代方案.

查找名为Program.cs的文件,其中包含以下代码片段...

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
Run Code Online (Sandbox Code Playgroud)

并改为

static class Program
{

    public static string[] CommandLineArgs { get; private set;}

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        CommandLineArgs = args;
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}
Run Code Online (Sandbox Code Playgroud)

然后从表单中访问命令行args ...

Program.CommandLineArgs
Run Code Online (Sandbox Code Playgroud)