0
votes

So I'm making a Notepad replica in my spare time just for fun, the core of the project is all done. However, I can't open my application from a file. For example, if you had a .txt file, you double click it and it would open the app (by default its Notepad).

I already know how to set the default application, but the code does not support opening the files yet, it can only open files from the menu inside the program.

How would I go about making it so that my application can be opened by files?

2

2 Answers

0
votes

in you void Main(string[] args) method, the args will contain the path to the file that was opened with your application. You can also get them anywhere in the application by calling string[] Environment.GetCommandLineArgs(). Print the argument in a message box to see what arguments have been passed.

Here's some code you might want to use for that:

//In a form

public void MyButton_Clicked(object sender, EventArgs e)
{
    string[] args = Environment.GetCommandLineArgs();
    MessageBox.Show(string.Join(Environment.NewLine, args));
}

0
votes

I like your question. Once you provide support for opening type files, and you find that your application is still not working, perhaps this might be useful: you can check under registry section HKEY_CLASSES_ROOT for the extension and action details. It talks about default applications, but it gets into more details.

Here's the page:

Finding the default application for opening a particular file type on Windows