使用AutoHotkey打开CMD命令行

1.以管理员模式打开CMD

#c::

    Run *RunAs cmd

    Return


2.在当前目录/文件夹下打开CMD

#c::

    dir := getExplorerPath() 

    Run, cmd, % dir

    Return

getExplorerPath() { ;获取路径

    If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") {

        WinHWND := WinActive()

        For win in ComObjCreate("Shell.Application").Windows

            If (win.HWND = WinHWND) {

                dir := SubStr(win.LocationURL, 9) ; remove "file:///"

                dir := RegExReplace(dir, "%20", " ")

                Break

            }

    }

    return % dir ? dir : A_Desktop

}

getExplorerPath2(){ ;也是获取路径

    if WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass"){

        IfWinExist, ahk_class CabinetWClass

            ControlGetText,address,ToolbarWindow323,ahk_class CabinetWClass

            StringLen, length, address

            StringRight, path, address, length-4

            return path

    }

    else{

        return A_Desktop

    }

}



3.如果是目录下则在当前目录下打开CMD,否则以管理员模式打开CMD

PS:哪位大神知道怎么实现以管理员模式在当前目录下打开CMD吗?

#c::CMD()

CMD() {

    If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") {

        WinHWND := WinActive()

        For win in ComObjCreate("Shell.Application").Windows

            If (win.HWND = WinHWND) {

                dir := SubStr(win.LocationURL, 9) ; remove "file:///"

                dir := RegExReplace(dir, "%20", " ")

                Break

            }

        Run cmd, % dir

    }

    else{

        Run *RunAs cmd

    }

}

你可能感兴趣的:(使用AutoHotkey打开CMD命令行)