Reputation: 334
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?
Upvotes: 0
Views: 719
Reputation: 664
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.
Upvotes: 1