Pyroblasted
Pyroblasted

Reputation: 23

C# - Opening Text Files With a Program

What i'm looking for is the functionality which all text editors have and that is to right click a file in windows and select open with and then select the text editor and pressing ok. The text in the file should then appear in a text box. I have searched everywhere but i have not found how to do this.

Edit: Some misunderstandings, i know how to set it as a default program in windows but i need to know how to make it open the text files with my program and then load that text file to a richtextbox.

Upvotes: 1

Views: 235

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40160

You need to check Environment.GetCommandLineArgs() for the command-line arguments sent to your program. Once you have added your program into the "Open with..." and you open a file or set of files with it, it will open your program sending an array of file paths that were selected in Windows Explorer.

You should handle the case where multiple files are selected, as they will be passed in. That method I linked returns a string array. If you want, you can just take the first one and ignore the rest. But also be sure that you test to see if there are any entries at all before you check the first one.

protected void Form_Load()
{
    var args = Environment.GetCommandLineArgs();
    if (args.Length > 0)
    {
        //open the file here...
    }
}

Upvotes: 3

Related Questions