0

I have created a context shell menu for / on .txt files.

Its 'action' is similar to that of 'Edit with notepad' option.

I am able to open 'notepad' on clicking the menu using this code -

subKey.SetValue("", "C:\\Windows\\System32\\notepad.exe");

 //subKey is the newly created sub key - The key creation part works fine.

How will I be able to use a feature similar to that of the 'Edit with notepad' feature? Or is it at least possible to get the name of the '.txt' file on which this event was triggered?

Note: By 'Edit with notepad', I mean viewing the selected file's contents in notepad.

user2053912
  • 173
  • 1
  • 3
  • 11

1 Answers1

3

The shell (explorer.exe) will substitute %1 with the file name. So you basically write:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\txtfile\shell\openwithnotepad]
@="Open with &Notepad"

[HKEY_CLASSES_ROOT\txtfile\shell\openwithnotepad\command]
@="\"C:\\Windows\\System32\\notepad.exe\" \"%1\""

The file name will be passed to C:\Windows\System32\notepad.exe as a command line argument. For example if you open D:\blah.txt, then notepad will receive D:\blah.txt as the first argument.

In C#, you basically use either Environment.GetCommandLineArgs() or args in Main to retrieve the file path.

An example:

string[] commandLineArgs = Environment.GetCommandLineArgs();
string fileToOpen = null;
if (commandLineArgs.Length > 1)
{
    if (File.Exists(commandLineArgs[1]))
    {
        fileToOpen = commandLineArgs[1];
    }
}
if (fileToOpen == null)
{
    // new file
}
else
{
    MyEditor.OpenFile(fileToOpen);
}
Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
  • I ran the registry in 'Administrator mode'. The key is being created, but I am unable to see the same on right clicking a .txt file. – user2053912 Apr 26 '13 at 15:39
  • Upon doing that, it says that the keys have been successfully added to the registry. Still, I don't see the `openwithnotepad` option. – user2053912 Apr 26 '13 at 15:47
  • Try manually editing the default value of `HKCR\txtfile\shell\openwithnotepad\command` to `"C:\Windows\System32\notepad.exe" "%1"`. – Alvin Wong Apr 26 '13 at 15:56
  • That is the value that is currently set. – user2053912 Apr 26 '13 at 15:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28992/discussion-between-user2053912-and-alvin-wong) – user2053912 Apr 26 '13 at 15:59