5

Working in C#, I would like to start a process and open a file in the default text editing program, which is not necessarily the default program association for that filetype.

For example, say I wanted to open an html file. The default association for that file may be firefox. How could I open it in the default text editor (e.g. Notepad, Notepad++, etc.)?

Thanks for your help.

edit: The comment below said there is no way to set a default text editing program. Very well, is there a way to pretend that the file is a txt file?

nonremovable
  • 798
  • 1
  • 6
  • 19
  • That concept doesn't exist. Default programs are only associated by file type or protocol. Have you ever seen a way to set your default text editing program without doing it by file type or protocol? – itsme86 Jan 15 '19 at 15:45
  • You use the lpClass to treat the file as a text file. Related: https://stackoverflow.com/questions/19445960/using-shellexecuteex-to-open-an-executable-also-specifying-an-lpclass – Raymond Chen Jan 15 '19 at 16:01

1 Answers1

4

Its in the registry:

    string edit = (string)Registry.GetValue(@"HKEY_CLASSES_ROOT\SystemFileAssociations\text\shell\edit\command", null, null);

    edit = edit.Replace("%1", @"c:\temp.txt");

    Process.Start("cmd.exe", "/c " + edit);

This will invoke the command it uses when you rightclick->Edit a text file instead of rightclick->Open

Leo Bartkus
  • 1,925
  • 13
  • 17