Batch - Default Browser?

前端 未结 8 1140
北荒
北荒 2021-02-12 16:09

Is there a way for using a batch file to find the default browser on my computer?

8条回答
  •  走了就别回头了
    2021-02-12 16:39

    It is impossible to do this 100% correct in a batch file since the default command could come from a COM object and not a string in the registry (MayChangeDefaultMenu will force IContextMenu to be called for double-clicks and could change the default action)

    Here is some code that tries to do the right thing (The fallback verb is open, it really should be the first subkey, but I did not feel like dealing with that)

    @echo off
    setlocal ENABLEEXTENSIONS
    set progid=htmlfile&set verb=open&set browsercmd=
    FOR /F "skip=2 tokens=2 delims=_" %%a IN ('2^>nul REG QUERY "HKCR\.html" /ve^|find /V ""^|find /V "HKEY_"') DO FOR /F "tokens=1,*" %%b IN ("%%~a") DO if not "%%~c"=="" set progid=%%~c
    FOR /F "skip=2 tokens=2 delims=_" %%a IN ('2^>nul REG QUERY "HKCR\%progid%\shell" /ve^|find /V ""^|find /V "HKEY_"') DO FOR /F "tokens=1,*" %%b IN ("%%~a") DO if not "%%~c"=="" set verb=%%~c
    FOR /F "skip=2 tokens=2 delims=_" %%a IN ('2^>nul REG QUERY "HKCR\%progid%\shell\%verb%\command" /ve^|find /V ""^|find /V "HKEY_"') DO FOR /F "tokens=1,*" %%b IN ("%%~a") DO if not "%%~c"=="" set browsercmd=%%c
    
    echo.DefaultBrowser=%browsercmd%
    

    This code probably has problems, but at least it tries to find the correct verb. You also have to deal with the fact that the returned string could contain "%1".

    If all you really want to do is open a URL, all you need is start http://example.com If you want to open the browser, but not a specific URL, a ugly hack like start "" http://about:blank might just work.

提交回复
热议问题