using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Microsoft.Win32; /// /// 注册表管理 /// public class RegistryManager : MonoBehaviour { /// /// 注册表文件名(前端通过这个KeyName调用) /// public string KeyName; // Start is called before the first frame update void Start() { AddRegist(KeyName); } // Update is called once per frame void Update() { } void AddRegist(string FileName) { if (FileName == "") { } else { try { //程序编号 string strPrimaryKey = FileName.Trim(); RegistryKey key = Registry.ClassesRoot; RegistryKey regKey = key.OpenSubKey(strPrimaryKey, true); if (regKey != null) { key.DeleteSubKeyTree(strPrimaryKey, true); key.Close(); } //文件项 RegistryKey regPrimaryKey = key.CreateSubKey(strPrimaryKey); regPrimaryKey.SetValue("", strPrimaryKey + " Protocol"); regPrimaryKey.SetValue("URL Protocol", ""); //默认图标 RegistryKey regDefaultIconKey = key.CreateSubKey(strPrimaryKey + "\\DefaultIcon"); //string strExePathName = Application.StartupPath + "\\" + Application.ProductName; string strExePathName = System.Environment.CurrentDirectory + "\\" + Application.productName; regDefaultIconKey.SetValue("", strExePathName + ",1"); RegistryKey regshellKey = key.CreateSubKey(strPrimaryKey + "\\shell"); RegistryKey regshellopenKey = key.CreateSubKey(strPrimaryKey + "\\shell\\open"); RegistryKey regshellopencommandKey = key.CreateSubKey(strPrimaryKey + "\\shell\\open\\command"); regshellopencommandKey.SetValue("", string.Format("\"{0}\" \"%1\"", strExePathName)); key.Close(); Debug.Log("生成注册表成功!"); } catch (Exception ex) { Debug.Log("生成注册表失败!"+ex.Message); } } } void DeleteRegist(string fileName) { try { if (string.IsNullOrEmpty(fileName.Trim())) { Debug.Log( "请输入程序编号!"); return; } string strPrimaryKey = fileName.Trim(); RegistryKey delKey = Registry.ClassesRoot; RegistryKey regPrimaryKey = delKey.OpenSubKey(strPrimaryKey, true); //判断要删除的regPrimaryKey是否存在 if (regPrimaryKey != null) { delKey.DeleteSubKeyTree(strPrimaryKey, true); } delKey.Close(); Debug.Log("删除注册表成功!"); } catch (Exception ex) { Debug.Log("删除注册表失败!"+ex.Message); } } }