0

I'm making a program which saves text documents and also opens them in the program in a textbox, the program works fine and I've managed to make it so I can save the files, and open them when giving the file name

However, I want to know if there's a way to open up explorer, then click the file and have it open in the program, I know Process.Start("explorer.exe"); is used to open explorer, but the files i open from there open in their default programs

If you know how to make it so text documents open in my program please let me know.

pnuts
  • 58,317
  • 11
  • 87
  • 139
user2981153
  • 15
  • 1
  • 4
  • You'd have to add yourself as a handler for each type of file you want to open with your program in the registry. But as a general rule, this sounds like a *really* bad idea. – Jeff B Nov 11 '13 at 23:37
  • Also, there are about 3 questions that are a duplicate of this. Check them out, you'll find your answer: [one](http://stackoverflow.com/q/222561/945456), [two](http://stackoverflow.com/q/5450387/945456), [three](http://stackoverflow.com/q/3788429/945456)... – Jeff B Nov 11 '13 at 23:40
  • Oh okay, so there's no simple way of doing it? Thanks for the links, I looked around and couldnt find any answers to my question, sorry for the dup post – user2981153 Nov 11 '13 at 23:44

2 Answers2

2

I think you need an OpenFileDialog control. which is basically a mini explorer that saves the file name you select in a property. Something like this should work:

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    DialogResult result = ofd.ShowDialog();
    if(result != DialogResult.Cancel)
    {
         richTextBox1.Text = File.ReadAllText(ofd.FileName);
    }
}
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
0

There's not really an easy way to do what you want (windows explorer integration and change the default 'open with' program). Even if it was easy, it'd be considered bad practice.

If you need your program open a file, the best option is to allow an user to open the file from the program. There's a control called OpenFileDialog in the tool box (assuming you're using WinForms that can do this). Work through this tutorial. It's a bit old, but should get you started.

Jeff B
  • 8,572
  • 17
  • 61
  • 140