2013-03-10 81 views
3

我想从其他机器的注册表文件中读取数据。基本上我有其他系统的硬盘驱动器,我可以从中拷贝或直接读取SYSTEM文件(Windows/system32/config/SYSTEM),这样我就可以从USBStor键(和其他东西)。导航注册表文件

请注意我不是试图读取从注册表中导出的.REG文件,也不是试图从本地机器读取配置单元。 ;-)

我一直在试图找到任何类型的库或本地.Net的方式来做到这一点,最好是免费的!读取.REG文件有很多参考,但不包括从其他系统获取的“平面”文件。

以前有人遇到过这个吗?

+0

你不妨看看[remote regi stry service](http://technet.microsoft.com/en-us/library/cc754820.aspx) – Blorgbeard 2013-03-10 22:49:43

+0

这些系统已死亡。我有物理访问他们的硬盘驱动器。我无法在网络上访问它们。 – rune711 2013-03-11 22:57:18

回答

0

您需要使用RegistryKey.OpenRemoteBaseKey方法解释here。请注意,根据链接MSDN文档:

为了一键远程开启,服务器和客户端 机必须运行远程注册表服务,并启用了远程 管理。

要启用远程注册表服务,使用Blorgbeard注释中的链接:http://technet.microsoft.com/en-us/library/cc754820.aspx

这里有一个例子:

 RegistryKey FetchedRemoteMachineKey; 

FetchedRemoteMachineKey = RegistryKey.OpenRemoteBaseKey(
          RegistryHive.CurrentUser, RemoteMachineName).OpenSubKey(
          "Machine"); 
+0

对不起,这些系统不在网络上。我可以物理访问硬盘及其文件。 – rune711 2013-03-11 22:58:36

2

退房RegLoadKey()(MSDN here),你应该能够做这样的事情:

using System.Runtime.InteropServices; 
using Microsoft.Win32; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 

    [DllImport("advapi32.dll")] 
    public static extern int RegLoadKey(uint hKey, string lpSubKey, string lpFile); 
    [DllImport("advapi32.dll")] 
    public static extern int RegUnLoadKey(uint hKey, string lpSubKey); 
    [DllImport("advapi32.dll")] 
    public static extern int OpenProcessToken(int ProcessHandle, int DesiredAccess, ref int tokenhandle); 
    [DllImport("kernel32.dll")] 
    public static extern int GetCurrentProcess(); 
    [DllImport("advapi32.dll")] 
    public static extern int AdjustTokenPrivileges(int tokenhandle, int disableprivs, [MarshalAs(UnmanagedType.Struct)]ref TOKEN_PRIVILEGES Newstate, int bufferlength, int PreivousState, int Returnlength); 
    [DllImport("advapi32.dll")] 
    public static extern int LookupPrivilegeValue(string lpsystemname, string lpname, [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid); 


    [StructLayout(LayoutKind.Sequential)] 
    public struct LUID 
    { 
     public int LowPart; 
     public int HighPart; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    public struct TOKEN_PRIVILEGES 
    { 
     public LUID Luid; 
     public int Attributes; 
     public int PrivilegeCount; 
    } 

    static void Main(string[] args) 
    { 
     int TOKEN_ADJUST_PRIVILEGES = 0x00000020; 
     int SE_PRIVILEGE_ENABLED = 0x00000002; 
     int TOKEN_QUERY = 0x00000008; 
     int token = 0; 
     int retval = 0; 
     uint HKU = 0x80000003; 
     string SE_BACKUP_NAME = "SeBackupPrivilege"; 
     string SE_RESTORE_NAME = "SeRestorePrivilege"; 

     string tmpHive = "offlineSystemHive"; 
     string offlineHive = "E:\\Windows\\system32\\config\\SYSTEM"; 

     LUID RestoreLuid = new LUID(); 
     LUID BackupLuid = new LUID(); 

     TOKEN_PRIVILEGES TP = new TOKEN_PRIVILEGES(); 
     TOKEN_PRIVILEGES TP2 = new TOKEN_PRIVILEGES(); 

     retval = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref token); 
     retval = LookupPrivilegeValue(null, SE_RESTORE_NAME, ref RestoreLuid); 
     retval = LookupPrivilegeValue(null, SE_BACKUP_NAME, ref BackupLuid); 

     TP.PrivilegeCount = 1; 
     TP.Attributes = SE_PRIVILEGE_ENABLED; 
     TP.Luid = RestoreLuid; 
     TP2.PrivilegeCount = 1; 
     TP2.Attributes = SE_PRIVILEGE_ENABLED; 
     TP2.Luid = BackupLuid; 

     retval = AdjustTokenPrivileges(token, 0, ref TP, 1024, 0, 0); 
     retval = AdjustTokenPrivileges(token, 0, ref TP2, 1024, 0, 0); 

     int rtnVal = RegLoadKey(HKU, tmpHive, offlineHive); 
     Console.WriteLine(rtnVal); //should be 0 

     RegistryKey baseKey = Registry.Users.OpenSubKey("offlineSystemHive\\ControlSet001\\Control\\ComputerName\\ComputerName"); 
     Console.WriteLine(baseKey.GetValue("ComputerName")); 
     baseKey.Close(); 

     rtnVal = RegUnLoadKey(HKU, tmpHive); 
     Console.WriteLine(rtnVal); //should be 0 
    } 
} 
} 
+0

这是否适合你? – raney 2013-03-14 03:49:43

+0

拉尼,抱歉,迟迟没有回来:不,我似乎无法得到它的工作。上面的代码运行顺利,但是当我创建“baseKey”时,它是空的。调用RegLoadKey时不会引发错误。你的方法正是我想要实现的。 – rune711 2013-03-17 21:26:56

+0

RegLoadKey返回“1314”,这是一个权限问题?我已确认我已经拥有了Windows目录的所有权,并且正在使用管理员权限运行我的应用程序。思考? – rune711 2013-03-17 22:16:09