Search code examples
visual-studioautohotkeyexplorer

Visual Studio environment variables script


I would like testing and feedback (I'm hoping this is the correct place for this mods please move/delete as appropriate)

As anyone who alters environment variables frequently knows Visual studio doesn't pick them up automatically.

I found exiting and restarting all instances of explorer.exe gets Visual Studio picking up the latest set of environment variables without a restart.

Unfortunately this means you lose all your open windows.

I have written an AutoHotKey (www.AutoHotKey.com) script to get around this.

; Array must be initialised
Full_Path := Object()

; First Array dimension must be setup
Full_Path[j] := A_LoopField

; Generate a list of HWND's for explorer windows
WinGet, id, list, ahk_class CabinetWClass

; iterate over all HWND's filling in our Full_Path array
Loop, %id%
{
    ; store hwnd in this_id for current loop
    this_id := id%A_Index%

    ; Get the window information for this_id
    WinGetText, pathToStore, ahk_id %this_id%

    ; strips the address out of the text storing in ActiveAddress
    StringSplit, ActiveAddress, pathToStore, `n

    ; Turn's Path Into Variable
    pathToStore = %ActiveAddress1%

    ; Remove's The Beginning "Address:" Phrase
    pathToStore := RegExReplace(pathToStore, "^Address: ", "")

    ; Remove's Carriage Returns Incase it Exist in pathToStore
    StringReplace, pathToStore, pathToStore, `r, , all

    ; Store the result in the Full_Path array
    ifExist, %pathToStore%
        Full_Path%A_Index% := pathToStore
}

; We can now kill all instances of explorer.exe at command prompt
Loop, %id%
{
    ; Store hwnd in id array
    this_id := id%A_Index%

    ; get process id to kill from stored hwnd
    WinGet, pidVal, PID, ahk_id %this_id%

    ; kill the explorer process
    Run, taskkill /f /pid %pidVal%
}
; kill explorer shell
RunWait, taskkill /f /im explorer.exe

; restart explorer shell
Run, explorer

; open all windows we had open previously
Loop, %id%
{
    ; store actual path to open in local variable path To Open
    pathToOpen := Full_Path%A_Index%

    ; Run explorer providing the correct path to open
    Run, explorer %pathToOpen%
}
Return

Any advice or improvements you can make would be greatly appreciated. Hopefully it will be of use to someone else.


Solution

  • Another approach:

    ; Get fullpath of all opened explorer windows:
    If WinExist("ahk_class CabinetWClass") ; explorer
    {
        list := ""
        ; https://autohotkey.com/boards/viewtopic.php?p=28751#p28751
        for window in ComObjCreate("Shell.Application").Windows
        {
           explorer_path := ""
           try explorer_path := window.Document.Folder.Self.Path 
           list .= explorer_path ? explorer_path "`n" : "" 
        }
        list := trim(list, "`n")
        ; MsgBox, "%list%"
    }
    
    ; We can now restart the Explorer.exe Process:
    RunWait, %comspec% /c taskkill /f /im explorer.exe ,,hide
    Process, WaitClose, explorer.exe
    Run, explorer.exe
    
    ; open all explorer windows we had open previously:
    If (list != "")
    {
        Process, wait, explorer.exe
        Loop, parse, list, `n
            Run %A_LoopField% 
    }