How to launch Windows' RegEdit with certain path?

浪子不回头ぞ 提交于 2019-11-27 05:27:52

问题


How do I launch Windows' RegEdit with certain path located, like "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0", so I don't have to do the clicking?

What's the command line argument to do this? Or is there a place to find the explanation of RegEdit's switches?


回答1:


There's a program called RegJump, by Mark Russinovich, that does just what you want. It'll launch regedit and move it to the key you want from the command line.

RegJump uses (or at least used to) use the same regedit window on each invoke, so if you want multiple regedit sessions open, you'll still have to do things the old fashioned way for all but the one RegJump has adopted. A minor caveat, but one to keep note of, anyway.




回答2:


Use the following batch file (add to filename.bat):

REG ADD HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit /v LastKey /t REG_SZ /d Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Veritas\NetBackup\CurrentVersion\Config /f
START regedit

to replace:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Veritas\NetBackup\CurrentVersion\Config

with your registry path.




回答3:


From http://windowsxp.mvps.org/jumpreg.htm (I have not tried any of these):

When you start Regedit, it automatically opens the last key that was viewed. (Registry Editor in Windows XP saves the last viewed registry key in a separate location). If you wish to jump to a particular registry key directly without navigating the paths manually, you may use any of these methods / tools.

Option 1
Using a VBScript: Copy these lines to a Notepad document as save as registry.vbs

'Launches Registry Editor with the chosen branch open automatically
'Author  : Ramesh Srinivasan
'Website: http://windowsxp.mvps.org

Set WshShell = CreateObject("WScript.Shell")
Dim MyKey
MyKey = Inputbox("Type the Registry path")
MyKey = "My Computer\" & MyKey
WshShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\Lastkey",MyKey,"REG_SZ"
WshShell.Run "regedit", 1,True
Set WshShell = Nothing

Double-click Registry.vbs and then type the full registry path which you want to open.

Example: HKEY_CLASSES_ROOT\.MP3

Limitation: The above method does not help if Regedit is already open.

Note: For Windows 7, you need to replace the line MyKey = "My Computer\" & MyKey with MyKey = "Computer\" & MyKey (remove the string My). For a German Windows XP the string "My Computer\" must be replaced by "Arbeitsplatz\".

Option 2
Regjump from Sysinternals.com

This little command-line applet takes a registry path and makes Regedit open to that path. It accepts root keys in standard (e.g. HKEY_LOCAL_MACHINE) and abbreviated form (e.g. HKLM).

Usage: regjump [path]

Example: C:\Regjump HKEY_CLASSES_ROOT\.mp3

Option 3
12Ghosts JumpReg from 12ghosts.com

Jump to registry keys from a tray icon! This is a surprisingly useful tool. You can manage and directly jump to frequently accessed registry keys. Unlimited list size, jump to keys and values, get current key with one click, jump to key in clipboard, jump to same in key in HKCU or HKLM. Manage and sort keys with comments in an easy-to-use tray icon menu. Create shortcuts for registry keys.




回答4:


I'd also like to note that you can view and edit the registry from within PowerShell. Launch it, and use set-location to open the registry location of your choice. The short name of an HKEY is used like a drive letter in the file system (so to go to HKEY_LOCAL_MACHINE\Software, you'd say: set-location hklm:\Software).

More details about managing the registry in PowerShell can be found by typing get-help Registry at the PowerShell command prompt.




回答5:


Here is one more batch file solution with several enhancements in comparison to the other batch solutions posted here.

It also sets string value LastKey updated by Regedit itself on every exit to show after start the same key as on last exit.

@echo off
setlocal EnableDelayedExpansion
set "RootName=Computer"

if not "%~1"=="" (
    set "RegKey=%~1"
    goto PrepareKey
)

echo/
echo Please enter the path of the registry key to open.
echo/
set "RegKey="
set /P "RegKey=Key path: "

rem Exit batch file without starting Regedit if nothing entered by user.
if "!RegKey!"=="" goto ExitBatch

:PrepareKey
rem Remove square brackets and double quotes from entered key path.
set "RegKey=!RegKey:"=!"
if "!RegKey!"=="" goto ExitBatch
set "RegKey=!RegKey:[=!"
if "!RegKey!"=="" goto ExitBatch
set "RegKey=!RegKey:]=!"
if "!RegKey!"=="" goto ExitBatch

rem Replace hive name abbreviation by appropriate long name.
set "Abbreviation=%RegKey:~0,4%"
if /I "%Abbreviation%"=="HKCC" (
    set "RegKey=HKEY_CURRENT_CONFIG%RegKey:~4%"
    goto GetRootName
)
if /I "%Abbreviation%"=="HKCR" (
    set "RegKey=HKEY_CLASSES_ROOT%RegKey:~4%"
    goto GetRootName
)
if /I "%Abbreviation%"=="HKCU" (
    set "RegKey=HKEY_CURRENT_USER%RegKey:~4%"
    goto GetRootName
)
if /I "%Abbreviation%"=="HKLM" (
    set "RegKey=HKEY_LOCAL_MACHINE%RegKey:~4%"
    goto GetRootName
)
if /I "%RegKey:~0,3%"=="HKU" (
    set "RegKey=HKEY_USERS%RegKey:~3%"
)

:GetRootName
rem Try to determine automatically name of registry root.
for /F "tokens=1,2*" %%K in ('%SystemRoot%\System32\reg.exe query "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey"') do (
    if /I "%%K"=="LastKey" (
        for /F "delims=\" %%N in ("%%M") do set "RootName=%%N"
    )
)

rem Is Regedit already running?
%SystemRoot%\System32\tasklist.exe | %SystemRoot%\System32\findstr.exe /B /I /L regedit.exe >nul
if errorlevel 1 goto SetRegPath

echo/
echo Regedit is already running. Path can be set only when Regedit is not running.
echo/
set "Choice=N"
set /P "Choice=Kill Regedit (y/N): "
if /I "!Choice!"=="y" (
    %SystemRoot%\System32\taskkill.exe /IM regedit.exe >nul 2>nul
    goto SetRegPath
)
echo Switch to running instance of Regedit without setting entered path.
goto StartRegedit

:SetRegPath
rem Add this key as last key to registry for Regedit.
%SystemRoot%\System32\reg.exe add "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "%RootName%\%RegKey%" /f >nul 2>nul

:StartRegedit
start /B regedit.exe

:ExitBatch
endlocal

The enhancements are:

  1. Registry path can be passed also as command line parameter to the batch script.

  2. Registry path can be entered or pasted with or without surrounding double quotes.

  3. Registry path can be entered or pasted or passed as parameter with or without surrounding square brackets.

  4. Registry path can be entered or pasted or passed as parameter also with an abbreviated hive name (HKCC, HKCU, HKCR, HKLM, HKU).

  5. Batch script checks for already running Regedit as registry key is not shown when starting Regedit while Regedit is running already. The batch user is asked if running instance should be killed to restart it for showing entered registry path. If the batch user chooses not to kill Regedit, Regedit is started without setting entered path resulting (usually) in just getting Regedit window to foreground.

  6. The batch file tries to automatically get name of registry root which is on English Windows XP My Computer, on German Windows XP, Arbeitsplatz, and on Windows 7 just Computer. This could fail if the value LastKey of Regedit is missing or empty in registry. For this case please set the right root name in third line of the batch code.




回答6:


Copy the below text and save it as a batch file and run

@ECHO OFF
SET /P "showkey=Please enter the path of the registry key: "
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "%showkey%" /f
start "" regedit

Input the path of the registry key you wish to open when the batch file prompts for it, and press Enter. Regedit opens to the key defined in that value.




回答7:


I thought this C# solution might help:

By making use of an earlier suggestion, we can trick RegEdit into opening the key we want even though we can't pass the key as a parameter.

In this example, a menu option of "Registry Settings" opens RegEdit to the node for the program that called it.

Program's form:

    private void registrySettingsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        string path = string.Format(@"Computer\HKEY_CURRENT_USER\Software\{0}\{1}\",
                                    Application.CompanyName, Application.ProductName);

        MyCommonFunctions.Registry.OpenToKey(path);

    }

MyCommonFunctions.Registry

    /// <summary>Opens RegEdit to the provided key
    /// <para><example>@"Computer\HKEY_CURRENT_USER\Software\MyCompanyName\MyProgramName\"</example></para>
    /// </summary>
    /// <param name="FullKeyPath"></param>
    public static void OpenToKey(string FullKeyPath)
    {
        RegistryKey rKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Applets\Regedit", true);
        rKey.SetValue("LastKey",FullKeyPath);

        Process.Start("regedit.exe");
    }

Of course, you could put it all in one method of the form, but I like reusablity.




回答8:


Create a BAT file using clipboard.exe and regjump.exe to jump to the key in the clipboard:

clipboard.exe > "%~dp0clipdata.txt"
set /p clipdata=input < "%~dp0clipdata.txt"
regjump.exe %clipdata%

( %~dp0 means "the path to the BAT file" )




回答9:


Building on lionkingrafiki's answer, here's a more robust solution that will accept a reg key path as an argument and will automatically translate HKLM to HKEY_LOCAL_MACHINE or similar as needed. If no argument, the script checks the clipboard using the htmlfile COM object invoked by a JScript hybrid chimera. The copied data will be split and tokenized, so it doesn't matter if it's not trimmed or even among an entire paragraph of copied dirt. And finally, the key's existence is verified before LastKey is modified. Key paths containing spaces must be within double quotes.

@if (@CodeSection == @Batch) @then
:: regjump.bat
@echo off & setlocal & goto main

:usage
echo Usage:
echo   * %~nx0 regkey
echo   * %~nx0 with no args will search the clipboard for a reg key
goto :EOF

:main
rem // ensure variables are unset
for %%I in (hive query regpath) do set "%%I="

rem // if argument, try navigating to argument.  Else find key in clipboard.
if not "%~1"=="" (set "query=%~1") else (
    for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0"') do (
        set "query=%%~I"
    )
)

if not defined query (
    echo No registry key was found in the clipboard.
    goto usage
)

rem // convert HKLM to HKEY_LOCAL_MACHINE, etc. while checking key exists
for /f "delims=\" %%I in ('reg query "%query%" 2^>NUL') do (
    set "hive=%%~I" & goto next
)

:next
if not defined hive (
    echo %query% not found in the registry
    goto usage
)

rem // normalize query, expanding HKLM, HKCU, etc.
for /f "tokens=1* delims=\" %%I in ("%query%") do set "regpath=%hive%\%%~J"
if "%regpath:~-1%"=="\" set "regpath=%regpath:~0,-1%"

rem // https://stackoverflow.com/a/22697203/1683264
>NUL 2>NUL (
    REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit"^
        /v "LastKey" /d "%regpath%" /f
)

echo %regpath%

start "" regedit
goto :EOF

@end // begin JScript hybrid chimera
// https://stackoverflow.com/a/15747067/1683264
var clip = WSH.CreateObject('htmlfile').parentWindow.clipboardData.getData('text');

clip.replace(/"[^"]+"|\S+/g, function($0) {
    if (/^\"?(HK[CLU]|HKEY_)/i.test($0)) {
        WSH.Echo($0);
        WSH.Quit(0);
    }
});



回答10:


Here is a simple PowerShell function based off of this answer above https://stackoverflow.com/a/12516008/1179573

function jumpReg ($registryPath)
{
    New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" `
                     -Name "LastKey" `
                     -Value $registryPath `
                     -PropertyType String `
                     -Force

    regedit
}

jumpReg ("Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run") | Out-Null

The answer above doesn't actually explain very well what it does. When you close RegEdit, it saves your last known position in HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit, so this merely replaces the last known position with where you want to jump, then opens it.




回答11:


This seems horribly out of date, but Registration Info Editor (REGEDIT) Command-Line Switches claims that it doesn't support this.




回答12:


You can make it appear like regedit does this behaviour by creating a batch file (from the submissions already given) but call it regedit.bat and put it in the C:\WINDOWS\system32 folder. (you may want it to skip editting the lastkey in the registry if no command line args are given, so "regedit" on its own works as regedit always did) Then "regedit HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0" will do what you want.

This uses the fact that the order in PATH is usually C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem; etc



来源:https://stackoverflow.com/questions/137182/how-to-launch-windows-regedit-with-certain-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!