Jason Rowe

Be curious! Choose your own adventure.

Right Click Add To Path

I do lots of installs on my windows machine and got tired of having to change the path environment variables manually. I created a AddPath.exe that will do this for me and added a short cut to the right click context menu.

In one right click I can get this result:

Here is how it works if you are interested:

  1. Compile Code Via – [email protected]:JasonRowe/AddPath.git.
  2. Put the executable (AddPath.exe) and the dependency file (System.dll) into C:\tools\
  3. This executable needs to run as Admin. Right click on AddPath.exe, click compatibility tab and select “run this program as an administrator”

To add the right click context menu you need to add a new key to the registry

  1. open the registry editor (Start -> Run -> regedit.exe)
  2. Find the key HKEY_CLASSES_ROOT\Folder. Create a new key named shell if its not already there (HKEY_CLASSES_ROOT\Folder\shell)
  3. In the shell key (possibly just created) create a new key and change its name AddPath or whatever you want to show in the context menu
  4. Under the new key create a new key named command (HKEY_CLASSES_ROOT\Folder\shell\AddPath\command) and change its default value C:\tools\AddPath.exe “%1”

That %1 in quotes becomes the folder path and is passed to AddPath.exe as a parameter. Here is what the code is doing with the path that is passed in:

 public static void AddPathSegments(Options options)
        {
            try
            {
                string allPaths = Environment.GetEnvironmentVariable("PATH", options.Target);
                if (allPaths != null)
                    allPaths = options.PathSegment + "; " + allPaths;
                else
                    allPaths = options.PathSegment;
                Environment.SetEnvironmentVariable("PATH", allPaths, options.Target);

                Console.WriteLine("Added path segment: {0}", options.PathSegment);
            }
            catch (Exception ex)
            {
                Console.WriteLine("could not add path segment: {0} -  error {1}", 
                                   options.PathSegment, ex);
            }
        }

Posted

in

, ,

by

Tags:

Comments

One response to “Right Click Add To Path”

  1. lfree Avatar
    lfree

    Thanks very much! It’s very handy! :)

Leave a Reply

Your email address will not be published. Required fields are marked *