Defrag Tools #132 - PATH

Play Defrag Tools #132 - PATH

The Discussion

  • User profile image

    I totally forgot about this:  Years ago I threw together a tiny WinForms utility that broke down the %PATH% values and loaded them in a grid control, that a user could then sort/re-order/delete/add to by dragging/dropping from Explorer, and validate the entire thing (verify the folders actually exist, look for duplicated entries, etc) at the click a button.  It also split it into two groups (current user path vs system path).

    If I remember correctly, the only thing I never implemented was the very last step you'd naturally take--that is, write the final result to the actual environment.  I think the blocker was that I wasn't sure what was the "cleanest" way to programmatically make the change permanent, so I just had a button that copied the final string to the clipboard, which I then manually pasted into the System Properties/Advanced dialog box.

    Obviously, to write back the system-wide value, I'd probably have to run my utility as an admin, but I can live with that.  I know the values exist in the registry, but that feels like a hack.  They're also exposed in WMI (Win32_Environment), but the class doesn't expose any method to make a change.

    Chad/Andrew:  What do you figure ought to be the best way to commit changes to %PATH% programmatically?

  • User profile image

    I think for system/user variables management is easier to use something like Rapid Environment Editor.

  • User profile image

    @dandy: I'd just call C:\Windows\System32\setx.exe with the new value. Might not be the best way, but it's easy.

    setx PATH <newstring>

    setx /m PATH <newstring>

  • User profile image

    @windev:  I completely forgot about SETX.  You're right, it's probably the least "hack-y" way of doing it.  Now, if only I could find the source for my utility...

  • User profile image

    Take care when you move the the MSSQL Server Tools around, with this you might hit the older version of dtexec over the newer one. And the newer one can work with the old one, but not vice versa. So I suggest putting the 120\Tools\Binn above the 110\Tools\Binn. 

Add Your 2 Cents