During the course of my education in programming, there has been one question that's been bugging me. How can you create a program with a function that will open a program from Windows explorer. I can open a file from within my program, but what I don't know how to do is make it so that when I assign a certain file type to open automatically with my program, to code a function that is run to open the file clicked on in the explorer. For example, Windows stores the fact that Microsoft Word is the default program for opening .doc files, but what I don't know is how to write the function that would register with Windows as the one for opening the file. If anyone could help me, it would be greatly appreciated.

Recommended Answers

All 4 Replies

The file type ascociation is handled by the OS, not the program.
What you must do is create your program with a bit of code that allows for the file path to be passed as an argument.
Then ascociate the compiled executable with the filetype you will be using.
think of it like a unix/linux/dos terminal, say i want to use a program called 'program' in the standard bin folder to open a file called 'file.type' you would do:
program file.type
There are many variances on this, but that is basically how you need to think of this. Windows Explorer looks what '*.type' is ascociated with, adds the full file path to the end of that string, then executes that code. Simple :)

So if I just did something like

public void open(string path)
{
       //open file
}

and registered it with Windows, it would automatically use this function to open the file?

Not quite :)
Make a new console program and it will automatically generate some code for you to use.
You'll see the following line:

static void Main(string[] args)

This means that all arguments you send to the program are stored in 'args' :)

Thank you very much. After a little research and a lot of elbow grease, I got my program to load the filename. Thank you very much for all of your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.