FileTypeRegInfoClass.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. namespace Extensometer.BLL
  10. {
  11. public class FileTypeRegInfo
  12. {
  13. /// <summary>
  14. /// 目标类型文件的拓展名
  15. /// </summary>
  16. public string ExtendName;
  17. /// <summary>
  18. /// 目标文件类型说明
  19. /// </summary>
  20. public string Description;
  21. /// <summary>
  22. /// 目标类型文件关联的图标
  23. /// </summary>
  24. public string IcoPath;
  25. /// <summary>
  26. /// 打开目标类型文件的应用程序
  27. /// </summary>
  28. public string ExePath;
  29. public FileTypeRegInfo() { }
  30. public FileTypeRegInfo(string extendName)
  31. {
  32. this.ExtendName = extendName;
  33. }
  34. }
  35. public class FileTypeRegisterClass
  36. {
  37. /// <summary>
  38. /// 创建单例模型
  39. /// </summary>
  40. private static FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass();
  41. private FileTypeRegisterClass()
  42. { }
  43. public static FileTypeRegisterClass GetFileTypeRegister()
  44. {
  45. return fileTypeRegister;
  46. }
  47. /// <summary>
  48. /// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。
  49. /// </summary>
  50. private static void RegisterFileType(FileTypeRegInfo regInfo)
  51. {
  52. if (FileTypeRegistered(regInfo.ExtendName))
  53. {
  54. return;
  55. }
  56. string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType";
  57. RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName);
  58. fileTypeKey.SetValue("", relationName);
  59. fileTypeKey.Close();
  60. RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName);
  61. relationKey.SetValue("", regInfo.Description);
  62. RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");
  63. iconKey.SetValue("", regInfo.IcoPath);
  64. RegistryKey shellKey = relationKey.CreateSubKey("Shell");
  65. RegistryKey openKey = shellKey.CreateSubKey("Open");
  66. RegistryKey commandKey = openKey.CreateSubKey("Command");
  67. commandKey.SetValue("", regInfo.ExePath + " %1");
  68. relationKey.Close();
  69. }
  70. /// <summary>
  71. /// GetFileTypeRegInfo 得到指定文件类型关联信息
  72. /// </summary>
  73. private static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
  74. {
  75. if (!FileTypeRegistered(extendName))
  76. {
  77. return null;
  78. }
  79. FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName);
  80. string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
  81. RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName);
  82. regInfo.Description = relationKey.GetValue("").ToString();
  83. RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon");
  84. regInfo.IcoPath = iconKey.GetValue("").ToString();
  85. RegistryKey shellKey = relationKey.OpenSubKey("Shell");
  86. RegistryKey openKey = shellKey.OpenSubKey("Open");
  87. RegistryKey commandKey = openKey.OpenSubKey("Command");
  88. string temp = commandKey.GetValue("").ToString();
  89. regInfo.ExePath = temp.Substring(0, temp.Length - 3);
  90. return regInfo;
  91. }
  92. /// <summary>
  93. /// UpdateFileTypeRegInfo 更新指定文件类型关联信息
  94. /// </summary>
  95. private static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
  96. {
  97. if (!FileTypeRegistered(regInfo.ExtendName))
  98. {
  99. return false;
  100. }
  101. string extendName = regInfo.ExtendName;
  102. string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";
  103. RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName, true);
  104. relationKey.SetValue("", regInfo.Description);
  105. RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true);
  106. iconKey.SetValue("", regInfo.IcoPath);
  107. RegistryKey shellKey = relationKey.OpenSubKey("Shell");
  108. RegistryKey openKey = shellKey.OpenSubKey("Open");
  109. RegistryKey commandKey = openKey.OpenSubKey("Command", true);
  110. commandKey.SetValue("", regInfo.ExePath + " %1");
  111. relationKey.Close();
  112. return true;
  113. }
  114. /// <summary>
  115. /// FileTypeRegistered 指定文件类型是否已经注册
  116. /// </summary>
  117. private static bool FileTypeRegistered(string extendName)
  118. {
  119. RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
  120. if (softwareKey != null)
  121. {
  122. return true;
  123. }
  124. return false;
  125. }
  126. /// <summary>
  127. /// 检查并创建项目类型
  128. /// </summary>
  129. public void CheckAndCreateFileType()
  130. {
  131. if (!FileTypeRegisterClass.FileTypeRegistered(".Esm")) //如果文件类型没有注册,则进行注册
  132. {
  133. FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".Esm"); //文件类型信息
  134. fileTypeRegInfo.Description = "引伸计工程文件";
  135. fileTypeRegInfo.ExePath = Application.ExecutablePath;
  136. fileTypeRegInfo.ExtendName = ".Esm";
  137. fileTypeRegInfo.IcoPath = Application.ExecutablePath; //文件图标使用应用程序的
  138. FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass(); //注册
  139. FileTypeRegisterClass.RegisterFileType(fileTypeRegInfo);
  140. Process[] process = Process.GetProcesses(); //重启Explorer进程,使更新立即生效
  141. var p = (from proc in process
  142. where proc.ProcessName.Equals("explorer")
  143. select proc).FirstOrDefault();
  144. p.Kill();
  145. }
  146. else
  147. {
  148. FileTypeRegInfo fileTypeRegInfo = GetFileTypeRegInfo(".Esm");
  149. //若项目位置有变,则更新项目信息
  150. if(fileTypeRegInfo.ExePath != Application.ExecutablePath)
  151. {
  152. UpdateFileType();
  153. }
  154. }
  155. }
  156. /// <summary>
  157. /// 更新项目类型
  158. /// </summary>
  159. public void UpdateFileType()
  160. {
  161. FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".Esm"); //文件类型信息
  162. fileTypeRegInfo.Description = "引伸计工程文件";
  163. fileTypeRegInfo.ExePath = Application.ExecutablePath;
  164. fileTypeRegInfo.ExtendName = ".Esm";
  165. fileTypeRegInfo.IcoPath = Application.ExecutablePath; //文件图标使用应用程序的
  166. FileTypeRegisterClass fileTypeRegister = new FileTypeRegisterClass(); //注册
  167. FileTypeRegisterClass.UpdateFileTypeRegInfo(fileTypeRegInfo);
  168. Process[] process = Process.GetProcesses(); //重启Explorer进程,使更新立即生效
  169. var p = (from proc in process
  170. where proc.ProcessName.Equals("explorer")
  171. select proc).FirstOrDefault();
  172. p.Kill();
  173. }
  174. /// <summary>
  175. /// 文件路径转所在文件夹路径
  176. /// </summary>
  177. /// <param name="FilePath"></param>
  178. /// <returns></returns>
  179. public string FilePathToFlodePath(string FilePath)
  180. {
  181. string result = string.Empty;
  182. string[] results = FilePath.Split('\\');
  183. for (int i = 0; i < results.Count() - 1; i++)
  184. {
  185. if (i == 0)
  186. {
  187. result = results[0];
  188. }
  189. else
  190. {
  191. result = result + "\\" + results[i];
  192. }
  193. }
  194. return result;
  195. }
  196. }
  197. }