So my winform is associated with a custom file format (.abc). How do I have it so that if text.abc is clicked, the winform runs and gets text.abc's contents.
Is it simply passed in as an argument?
So my winform is associated with a custom file format (.abc). How do I have it so that if text.abc is clicked, the winform runs and gets text.abc's contents.
Is it simply passed in as an argument?
First you need to register your custom file extension, take a look at this post:
Associate File Extension with Application
Then inside your windows app you can read command arguments like this:
private void Form1_Load(object sender, EventArgs e)
{
foreach (var item in Environment.GetCommandLineArgs())
{
Debug.WriteLine(item);
}
}
If you have correctly registered your application for a particular extension, then if the user double click on a file with that extension, the SO invokes your application and passes a commandline argument with the file name double clicked.
The framework also initializes the Environment.CommandLine
property with the passed commandline.
You could retrieve this property everywhere in your program or use the Environment.GetCommandLineArgs
array. Just remember that this array has the first element equalt to the name of your program and the arguments starts at the second element.
You should write your main entry point with arguments like in a console application
static void Main(string[] args)
{
if(args.Length > 0)
{
// command line passed
string fileToProcess = args[0];
if(Path.GetExtension(fileToProcess) == ".abc")
{
// Whatever
}
}
}