I'm trying to write a registry hack that will streamline some of my setup tasks when I create a new VM or repave my laptop. One thing I'm doing is switching to portable versions (synced in Dropbox) of a bunch of my favorite apps, including Notepad++.

I want to create a registry entry to allow me to have the Open with Notepad++ option in my right click menu, which is the only thing I'm missing with the portable version. I'm getting an error, though:

Windows cannot access the specified device, path or file. You may not have the 
appropriate permissions to access the item.

Here's my .reg file:

[HKEY_CLASSES_ROOT\*\shell\Open with Notepad++]

[HKEY_CLASSES_ROOT\*\shell\Open with Notepad++\command]
@="%USERPROFILE%\\Dropbox\\Programs\\Notepad++\\notepad++.exe %1"

The icon works perfectly. I can get the entry above to work fine if I hardcode C:\Users\myusername, but I'm going for portability here.

I'm pretty sure the issue is just finding the right escape sequence for the % symbols, but it's hard to say.


EDIT: I went with the add reg command option, and after a bit of trial and error, here's the command that worked:

reg add HKCR\*\shell\OpenWithNotepad++\command /t REG_EXPAND_SZ /ve /d ^%USERPROFILE^%"\Dropbox\Programs\Notepad++\notepad++.exe %1"

After running the command, I exported the results as a .reg file as well. Wins all around.

| improve this question | | | | |

To use (expand) environment variables in the registry, the value must be of the type REG_EXPAND_SZ.

Based on this question, it would be easiest to add that kind of value with the reg command rather than a .reg file..

reg add <KEY> /v <NAME> /t REG_EXPAND_SZ /d <DATA>

See reg add /? for more information on this syntax.

You could also encode the data as hex. There's some examples of that here:

REG_EXPAND_SZ data must be presented as what MS calls a binary data type (subtype "2"), so the value must be formatted in a kind of hexidecimal format, comma-delimited, two tokens per byte (padded with zeros), with a terminating null byte of course (and further explanation is obviously far beyond the scope of this article, and the author won't be held responsible for anyone's misuse of the incomplete information given thus far). So the line above will not work but indicates the end result I wanted to achieve. As a real working .REG -file entry the example above must be rendered as:



| improve this answer | | | | |
  • This was very helpful. Stuck on the syntax of the command, though: reg add HKCR\*\shell\Open with Notepad++\command /t REG_EXPAND_SZ /ve /d ^%USERPROFILE^%\Dropbox\Programs\Notepad++\notepad++.exe %1 is generating a syntax error. Not really sure how to handle the space before the %1, either... – Josh Earl May 10 '12 at 3:08
  • 2
    Put quotes around the strings... spaces get split into separate arguments, which becomes invalid syntax (more arguments than expected). – Bob May 10 '12 at 3:10
  • As crazy as it sounds, you can just put quotes around the spaces within the string. reg add HKCR\*\shell\OpenWithNotepad++\command /t REG_EXPAND_SZ /ve /d ^%USERPROFILE^%\Dropbox\Programs\Notepad++\notepad++.exe" "%1 – jaylweb Jun 8 '16 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.