简体   繁体   English

C#没有参数传递给Main

[英]C# No Arguments Being Passed to Main

I have been trying to get my arguments to be passed to my main method in C#. 我一直在尝试将参数传递给C#中的main方法。 Mainly I just want to capture the file path that was double clicked. 主要是我只想捕获双击的文件路径。 I have my files with custom extension and when it runs it open my program. 我的文件带有自定义扩展名,当它运行时打开我的程序。 That part works. 那部分起作用。

    static string file = "";

    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();

        if (args.Length > 0) file = args[0];

        Application.Run(new Form1());
    }

    public Form1()
    {
        InitializeControls();
    }

I also tried this way, which is not much different. 我也尝试过这种方式,没有太大区别。

    static string file = "";

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();

        string[] args = Environment.GetCommandLineArgs();
        if (args.Length > 0) file = args[0];

        Application.Run(new Form1());
    }

    public Form1()
    {
        InitializeControls();
    }

Its worth mentioning that I have this in a partial class. 值得一提的是,我在部分课程中有这个。 I don't know if that effects it directly or not. 我不知道这是否直接影响到它。

I don't really need to get the args if I can just get the file that was double clicked but I feel as that is the only way, and now I am curious. 如果我只要双击就可以得到文件,那么我并不需要获取args,但是我觉得那是唯一的方法,现在我很好奇。

What am I missing that is preventing me from being able to pass args to my main? 我缺少什么使我无法将args传递给我的主设备?

if you run "ftype" in the command prompt your program type should appear as: 如果在命令提示符下运行“ ftype”,则程序类型应显示为:

YOURTYPE="yourProgram.exe" "%1"

If the "%1" doesn't appear in exactly that way, then the association is wrong (it won't pass the name of the file as an argument). 如果"%1"并非以这种方式出现,则关联是错误的(它不会将文件名作为参数传递)。 Give it a try. 试试看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM