0

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?

blapaz
  • 334
  • 2
  • 11

1 Answers1

1

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.

  • In the normal cmd on my computer? – blapaz Jul 15 '16 at 19:17
  • Yes. Just run the command in the command prompt and look for your program. – Milton Hernandez Jul 15 '16 at 19:23
  • I have a ton of stuff in there and I dont see the program at all. – blapaz Jul 15 '16 at 19:25
  • How did you register your program to work with the custom extension? If you run the command "assoc" do you see your extension? In general you should be able to do the following: a) Open the command prompt as an administrator b) run ASSOC .YOUREXT=YOURLABEL c) run FTYPE YOURLABEL="PATH\YourProgram.exe" "%1". After running this, your should be able to just click or type on a file with extension .YOUREXT and it should call your program and send the file name as a parameter. You can test this by association the ftype with, say Notepad.exe and seeing if the file opens in it. – Milton Hernandez Jul 15 '16 at 19:42
  • I do not see it. I set up it up by going into publish settings in visual studio and you can add an extension to open the application. I have a feeling that I that doesn't do it correctly then. But it does work. – blapaz Jul 15 '16 at 19:44
  • Try to configure it by hand in CMD as administrator – Milton Hernandez Jul 15 '16 at 19:47