0

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?

MK3GTX
  • 241
  • 4
  • 13
  • If I understand correctly, you need two things here: 1. associating .abc files with your program in Windows and 2. knowing how to retrieve the filename once your program is launched. Am I right? Or is it only the second point? – Patrice Gahide Sep 19 '14 at 17:54
  • It's more of the second part. Once a file is clicked and it's associated program is launched, how does that program get the file's info? Sorry for the ambiguity. – MK3GTX Sep 19 '14 at 17:55

2 Answers2

1

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);
        }
    }
Community
  • 1
  • 1
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46
1

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
        }

    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Perfect! That's exactly what I wanted to know. Thanks for your answer, but I'm going to have to give Gregor the "accepted answer" as he beat you by roughly 30 seconds. – MK3GTX Sep 19 '14 at 18:05