How to get the exact path to notepad.exe to link the file extension

I need to associate a file extension. I created ".rulog" using notepad.exe as part of installing the installation project for the Windows 7 machine (it is here because we need administrator rights to write to the registry).

Basically I need to programmatically get the exact path to notepad.exe. Now I understand that he usually lives in C: \ Windows \ system32. This is part of the PATH system environment variable, so I assume that I could iterate over all the PATH variables and check if "notepad.exe" exists by combining "notepad.exe" with the current path using File.Exists. However, this seems very awkward.

Essentially I need to add an entry to

Computer\HKEY_CLASSES_ROOT\.rulog\shell\open\command\ 

with the value of the path to the notebook.

By the way, I see .txt in:

 Computer\HKEY_CLASSES_ROOT\.txt\ShellNew 

matters to ItemName

 "@%SystemRoot%\system32\notepad.exe,-470" 

Perhaps I can just copy this value? Or is it dangerous? (For example, does not exist).

+6
source share
3 answers

You can use:

 Environment.GetEnvironmentVariable("windir") + "\\system32\\notepad.exe"; 

Or even simpler:

 Environment.SystemDirectory + "\\notepad.exe"; 

So it doesnโ€™t matter which drive os is on.

+8
source

Copying a value with% systemroot% should be great. If it works for the OS, it should work for you!

+3
source

Solution with stupid check:

 string NotepadPath = Environment.SystemDirectory + "\\notepad.exe"; if (System.IO.File.Exists(NotepadPath)) { Microsoft.Win32.Registry.SetValue("HKEY_CLASSES_ROOT\\.rulog\\shell\\open\\command\\", "", NotepadPath + " %1"); } else { //do something else or throw new ApplicationException("Notepad not found!"); } 
0
source

Source: https://habr.com/ru/post/905843/


All Articles