Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a file in the PC's default text editing software (not the default associated app)?

Tags:

c#

.net

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?

like image 770
nonremovable Avatar asked Jan 15 '19 15:01

nonremovable


People also ask

How do I open a default program?

Open Default Programs by clicking the Start button , and then clicking Default Programs. Use this option to choose which programs you want Windows to use, by default. If a program does not show up in the list, you can make the program a default by using Set Associations.

What is the default text editing software in Windows?

Windows Notepad is a simple text editor for Windows; it creates and edits plain text documents.


1 Answers

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

like image 128
Leo Bartkus Avatar answered Oct 15 '22 10:10

Leo Bartkus