I use the following code to pass the first argument (the one that contains the file name) to my gui application:
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(args.Length == 0 ? new Form1(string.Empty) : new Form1(args[0]));
}
}
I test to see if there is an argument. If not and a user starts your program without one then you may get an exception in any code that tries to use it.
This is a snippet from my Form1 that deals with the incoming file:
public Form1(string path) {
InitializeComponent();
if (path != string.Empty && Path.GetExtension(path).ToLower() != ".bgl") {
//Do whatever
} else {
MessageBox.Show("Dropped File is not Bgl File","File Type Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
path = string.Empty;
}
//.......
}
You will see that I am checking the extension sent in - my app only works with one extension type - .bgl - so if the user tries to open some other file extension then I stop them. In this case I am dealing with a dropped file. This code will also allow a user to drag a file over my executable (or related icon) and the program will execute wit hthat file
You might also consider creating a file association between your file extension and your program if you have not already. This in conjunction with the above will allow the user to double click on your file and have your application open it.