5

I need to associate a file extension I have created “.rulog” with notepad.exe as part of a setup project installation for a windows 7 machine (it’s here since we require admin privileges to write to the registry).

Basically I need to obtain programmatically the exact path of the notepad.exe. Now, I understand that it typically lives in C:\Windows\system32. This is part of PATH system environment variable, so I guess I could loop through all the PATH variables and test if “notepad.exe” exists by combining “notepad.exe” with the current path using File.Exists. However this feels very clumsy.

Essentially I need to add an entry to

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

with the value of the path of notepad.

Incidentally I can see that .txt in:

Computer\HKEY_CLASSES_ROOT\.txt\ShellNew

has a value for ItemName of

“@%SystemRoot%\system32\notepad.exe,-470”

Perhaps I can just copy this value? Or is this dangerous?(e.g. does not exist).

Jeb
  • 3,689
  • 5
  • 28
  • 45
  • 1
    Out of curiosity, why do some developers insist on creating their own propriety file extensions for something as simple as a text file? – KingCronus Jan 12 '12 at 17:08
  • 1
    Out of curiosity, if you know that notepad can open your files, why not use an equivalent existing file extension? – murgatroid99 Jan 12 '12 at 17:10
  • It's very likely an application will be written which will search the directory for these file types. We already have .txt/.log existing for other logging/info hence this is just a provision for it. – Jeb Jan 12 '12 at 17:16
  • @JohnParr in that case ".ru.log" would still be searchable. – default.kramer Jan 12 '12 at 18:07

3 Answers3

9

You can use:

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

Or even easier:

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

That way it doesn't matter which drive the os is on.

Bali C
  • 30,582
  • 35
  • 123
  • 152
3

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

John Gardner
  • 24,225
  • 5
  • 58
  • 76
0

Fool-proof solution:

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!");
}
Elmo
  • 6,409
  • 16
  • 72
  • 140