How to make a file load in my program when a user double clicks an associated file.
Posted
by Edward Boyle
on Techlexic
See other posts from Techlexic
or by Edward Boyle
Published on Mon, 22 Nov 2010 21:41:11 +0000
Indexed on
2010/12/06
17:00 UTC
Read the original article
Hit count: 577
c#
|Visual Studio
This is sometimes confusing because you just don’t think about it — you have to access a file that you rarely access when making Windows forms applications, “Program.cs
”
static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }
There are so many ways to skin this cat, so you get to see how I skinned my last cat.
static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Form1 mainf = new Form1(); if (args.Length > 0) { try { if (System.IO.File.Exists(args[0])) { mainf.LoadFile= args[0]; } } catch { MessageBox.Show("Could not open file.", "Could not open file.", MessageBoxButtons.OK, MessageBoxIcon.Information); } } Application.Run(mainf); } }
static void Main(string[] args)
this is not a part of the default program.cs
You will notice the mainf.LoadFile
property. In the main form of my program I have a property for public string LoadFile ...
and the field private string loadFile = String.Empty;
in the forms load event I check the value of this field.
private void Form1_Load(object sender, EventArgs e) { if(loadFile != String.Empty){ // The only way this field is NOT String.empty is if we set it in // static void Main() of program.cs // LOAD it however it is needed OpenFile, SetDatabase, whatever you use. } }
© Techlexic or respective owner