通过注册表获取应用程序的路径

发表于2017-10-11
评论0 1.8k浏览

获取应用程序的路径可以说是一个不大不小的需求,那么有什么好的方法来实现呢,正是下面要和大家介绍的通过注册表获取应用程序的路径,想知道的可以看看。


应用程序的路径虽然可以硬编码,例如,路径 ="e:\\Program Files\\Unity\\Editor\\Unity.exe",但是这不是最佳的方式,因为用户不可能将Unity安装到默认目录。  

     

Unity内部:EditorApplication.applicationPath     《=》E:/Program Files/Unity5/Editor/Unity.exe

(任意的程序都可以啦~)

这里说的是在Unity编辑器外,  不是在Unity编辑器中!

若要提取Unity 安装文件夹,其在 windows 平台上可以使用注册表,如下所示︰


正常的应用应该是在    HKEY_LOCAL_MACHINE\SOFTWARE 下, 但是找了一下,没有找到,    然后在   HKEY_CLASSES_ROOT  下 找到e:\Program Files\Unity\Editor\Unity.exe


但是Unity跟其他的应用不一样,  可以安装多个版本,  上图的路径只是最近安装的Unity的版本。

 

Python的    代码实现:

  1. from winreg import *  
  2.   
  3.    
  4.   
  5. def GetUnityRootPath():  
  6.   
  7.     aReg = ConnectRegistry(None, HKEY_CLASSES_ROOT)  
  8.   
  9.     aKey = OpenKey(aReg, r"Unity package file\DefaultIcon")  
  10.   
  11.     # aKey = OpenKey(aReg, r"Unity scene file")  
  12.   
  13.    
  14.   
  15.     # 获取 '(default)'    key 的值  
  16.   
  17.     value = QueryValueEx(aKey, "")  
  18.   
  19.     print(value[0])  
  20.   
  21.    
  22.   
  23.     # 获取'\\Editor\\Unity.exe' 的起始位置  
  24.   
  25.     substring = value[0]  
  26.   
  27.     pos = substring.find("\\Editor\\Unity.exe")  
  28.   
  29.     print(pos)  
  30.   
  31.     unityPath = substring[:pos]  
  32.   
  33.     if unityPath[0]=="\"":  
  34.   
  35.         unityPath = unityPath[1:pos]  
  36.   
  37.    
  38.   
  39.     print (unityPath)  
  40.   
  41.    
  42.   
  43. if __name__ == "__main__":  
  44.   
  45.     GetUnityRootPath();  


 

 

C# 的代码实现:

  1. using Microsoft.Win32;  
  2.   
  3. using System.IO;  
  4.   
  5. using System;  
  6.   
  7. class Program  
  8.   
  9. {  
  10.   
  11.     private static string GetUnityRootPath()  
  12.   
  13.     {  
  14.   
  15.         var regKey = @"Unity package file\DefaultIcon";  
  16.   
  17.         RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(regKey);  
  18.   
  19.    
  20.   
  21.         string pathName = (string)registryKey.GetValue(null);  // "(Default)"  
  22.   
  23.         if (string.IsNullOrEmpty(pathName))  
  24.   
  25.         {  
  26.   
  27.             return null;  
  28.   
  29.         }  
  30.   
  31.    
  32.   
  33.         int index = pathName.LastIndexOf(",");  
  34.   
  35.         if (index != -1)  
  36.   
  37.         {  
  38.   
  39.             var exepath = pathName.Substring(0, index).Replace("\""string.Empty);  
  40.   
  41.             var binpath = Path.GetDirectoryName(exepath);   //  
  42.   
  43.             var di = new DirectoryInfo(binpath);  
  44.   
  45.             if (di.Parent != null)  
  46.   
  47.             {  
  48.   
  49.                 return di.Parent.FullName;  
  50.   
  51.             }  
  52.   
  53.         }  
  54.   
  55.         return null;  
  56.   
  57.     }  
  58.   
  59.     static void Main(string[] args)  
  60.   
  61.     {  
  62.   
  63.         Console.WriteLine(GetUnityRootPath());  
  64.   
  65.     }  
  66.   
  67. }  


如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

标签: