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

标签 c# winforms command-line arguments

C#:我想将文件路径等消息传递到我的表单应用程序(如控制台应用程序),我该怎么做?

我被告知我需要找到我的主要方法来添加 string[] args,但我不知道在 Windows Forms 中应该是哪一个。在 C# Windows 窗体应用程序中,我的主要方法是什么?

最佳答案

好的,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());
    }
}

并将其更改为

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

然后从您的表单访问命令行参数 ...

Program.CommandLineArgs

关于C#:我想像控制台应用程序一样将文件路径等消息传递到我的表单应用程序,我该怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/541577/

相关文章:

c# - WinRT 信息 : Cannot find a resource with the given key. 转换器引用问题

c# - 如何在表单启动后执行代码?

command-line - 命令行输出到 TXT 文件 (Java.exe -version) 返回空白

c# - 在 Windows Phone 中自定义闹钟

c# - TeamCity 8 中的 DotCover 不起作用

c# - Fluent API 中具有常量的外键

c# - keydown 事件无法获取确切的长度

c# - 在带有参数的winforms webbrowser控件中从JS调用C#方法时出错

javascript - 如何通过命令行在 Windows 上卸载 node-gyp

java - 终端中没有显示 junit 结果(通过或失败)