简体   繁体   中英

C# No Arguments Being Passed to Main

I have been trying to get my arguments to be passed to my main method in C#. 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.

What am I missing that is preventing me from being able to pass args to my main?

if you run "ftype" in the command prompt your program type should appear as:

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). Give it a try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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