C#域内远程机文件信息注册表访问。

  1. 获取远程机IP地址。

代码
public string GetMachineIPAddress(string strMachineName)
        {
            
string strMachineIP;
            Ping oP 
= new Ping();   // Declare a ping hander object.
            PingReply oPR = oP.Send(strMachineName);    // Get the return values from ping method.
            strMachineIP = oPR.Address.ToString();  // Get the target machine IP address.
            return strMachineIP;
        }

 2. 判断系统类型32/64.

代码
public string GetMachineBit(string strMachineName)  // Return the remoting machine system bit number.
        {
            
string strReturnStatus;
            
string strAccessPath;
            
string sTempMCIP;
            
string[] aFiles;
            
bool bISBIT;

            strReturnStatus 
= "32";
            
try
            {
                
                sTempMCIP 
= GetMachineIPAddress(strMachineName); // Call local method to get the remote machine IP address.
                strAccessPath = @"\\"+ sTempMCIP + @"\\C$"// Access the remote serveer driver C:.
 
                aFiles 
= Directory.GetDirectories(strAccessPath);  // Access to the target machine c$\Program Files folder.
                for (int i = 0; i < aFiles.Length - 1; i++// Ergodic the folder info on remote driver C:
                { 
                    bISBIT 
= aFiles[i].ToString().Contains("Program Files (x86)");
                    
if (bISBIT) // It is a 64 bit system if contain the X86 folder.
                    { 
                        strReturnStatus 
= "64"// Return the judgement result.
                    }
                }
                 
                
return strReturnStatus;
            }
            
catch (Exception xe)
            { 
                strReturnStatus 
= xe.Message;
                
return strReturnStatus;
            }
            
        }

 3.  从注册表获取某个文件的安装路径信息。

代码
        public string GetRemoteMachinGenInstallPath(string sRemoteName)
        {
            
            remoteName 
= sRemoteName; 
            strRemoteMCGenInsPath = "No Geneva Install";
            
            
try
            {
                
if ((GetMachineBit(remoteName) == "32"))  //Call local method get the remote system bit info.
                {
                    
// The sub key path for 32bit system.
                    strKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
                }
                
else
                {
                    
if (GetRemoteOSVersion(remoteName).Contains("Microsoft Windows Server 2003")) // The WinServer2003 for 32 and 64 has the same key path, but other OS need access the WOW64 key node path.
                    {
                        strKeyPath 
= @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
                    }
                    
else
                    {
                        
// The sub key path for 64bit system.
                        strKeyPath = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
                        
//strKeyPath = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{6F13B97D-6FA6-49A3-90D6-653099B9E7A7}";
                    }
                }

                
// Open the remote machine uninstall node subkey.
                environmentKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, remoteName).OpenSubKey(strKeyPath);


                
if (environmentKey != null)
                {
                    lsRemoveSubKeys 
= environmentKey.GetSubKeyNames();  // Get the all the key name which under the uninstall node.
                    if (lsRemoveSubKeys.Length > 0)
                    {
                        
foreach (string uninstallKey in lsRemoveSubKeys) // Ergodic the keys under uninstall and pick up the subkey which has the Gen app install path attribute value.
                        { 
                            RegistryKey rkUninstallKey 
= environmentKey.OpenSubKey(uninstallKey);   // Open subkey node in order to get the attribute text.

                            
string installpath = (String)rkUninstallKey.GetValue("InstallLocation", String.Empty);  // Get the attribute value from InstallLocation node.
                            string strDisplayName = (String)rkUninstallKey.GetValue("DisplayName", String.Empty);   // Get the attribute value from DisplayName node.

                            
//string uninstallString = (String)rkUninstallKey.GetValue("UninstallString", String.Empty);
                            if (strDisplayName.Contains("Geneva Application Server"))// && !uninstallString.Contains("InstallShield Installation Information"))
                            {
                                
if (CheckRemoteMachineInstallPath(installpath, remoteName))
                                {
                                    strGenSubKey 
= uninstallKey;    // The install key node.
                                    strRemoteMCGenInsPath = installpath;    // The Gen app installed path on remote machine.    
                                }
                                
                            }
                        }
                    }
                }
                environmentKey.Close();
                
return  strRemoteMCGenInsPath;
            }
            
catch (Exception xe)
            {
                
string sXE = xe.Message;
                
return sXE;
            }
        }

4. 读取远程机某文件版本信息。

代码
        public string GetGenAppVersion(string sRemoteIP,string strDllFilePath)
        {
            
string sGenVersion;
            
try
            {
                
if (strDllFilePath == "No Geneva Install")
                {
                    sGenVersion 
= "N/A";
                }
                
else
                {

                     string sDllPath = "\\" + sRemoteIP + "\\" + strDllFilePath.Replace(":""$"+ "AppServer\\ConfigurationManager.exe";

                    FileVersionInfo myFileVersion 
= FileVersionInfo.GetVersionInfo("\\" + sDllPath);
                    
                    sGenVersion 
= myFileVersion.FileVersion;
                }
            }
            
catch (Exception xe)
            {
                
                sGenVersion 
="N/A";
            }
            
            
return sGenVersion; 
        }

 5. 远程计算机系统版信息。

代码
 public string GetRemoteOSVersion(string sRemoteName)
        {
            
string sVersion;
            sVersion 
= "NULL";
            strKeyPath 
= @"SOFTWARE\Microsoft\Windows NT";
            environmentKey 
= RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, sRemoteName).OpenSubKey(strKeyPath);
            lsRemoveSubKeys 
= environmentKey.GetSubKeyNames();

            
foreach (string uninstallKey in lsRemoveSubKeys) // Ergodic the keys under uninstall and pick up the subkey which has the Gen app install path attribute value.
            {
                RegistryKey rkUninstallKey 
= environmentKey.OpenSubKey(uninstallKey);   // Open subkey node in order to get the attribute text.
 
                sVersion 
= (String)rkUninstallKey.GetValue("ProductName", String.Empty);
            }
            
return sVersion;
        }
原文地址:https://www.cnblogs.com/zencorn/p/1693887.html