PageRenderTime 95ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/CKS Developer Edition-VS2012/CKS.Dev.Core/Environment/ProcessUtilities.cs

#
C# | 384 lines | 258 code | 29 blank | 97 comment | 17 complexity | ccdf4b3424e7976f15623d2f14824c58 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.SharePoint;
  6. using EnvDTE;
  7. using EnvDTE80;
  8. using System.ServiceProcess;
  9. using Microsoft.Win32;
  10. using CKSProperties = CKS.Dev.Core.Properties.Resources;
  11. using System.Diagnostics;
  12. using System.DirectoryServices;
  13. using System.IO;
  14. using System.Threading;
  15. #if VS2012Build_SYMBOL
  16. using CKS.Dev11.VisualStudio.SharePoint.Commands;
  17. #elif VS2013Build_SYMBOL
  18. using CKS.Dev12.VisualStudio.SharePoint.Commands;
  19. #elif VS2014Build_SYMBOL
  20. using CKS.Dev13.VisualStudio.SharePoint.Commands;
  21. #else
  22. using CKS.Dev.VisualStudio.SharePoint.Commands;
  23. #endif
  24. #if VS2012Build_SYMBOL
  25. namespace CKS.Dev11.VisualStudio.SharePoint.Environment
  26. #elif VS2013Build_SYMBOL
  27. namespace CKS.Dev12.VisualStudio.SharePoint.Environment
  28. #elif VS2014Build_SYMBOL
  29. namespace CKS.Dev13.VisualStudio.SharePoint.Environment
  30. #else
  31. namespace CKS.Dev.VisualStudio.SharePoint.Environment
  32. #endif
  33. {
  34. /// <summary>
  35. /// Process Utilities
  36. /// </summary>
  37. class ProcessUtilities
  38. {
  39. /// <summary>
  40. /// Gets or sets the current DTE.
  41. /// </summary>
  42. /// <value>The current DTE.</value>
  43. public DTE CurrentDTE { get; private set; }
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="ProcessUtilities"/> class.
  46. /// </summary>
  47. public ProcessUtilities()
  48. {
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="ProcessUtilities"/> class.
  52. /// </summary>
  53. /// <param name="dte">The DTE.</param>
  54. public ProcessUtilities(DTE dte)
  55. {
  56. CurrentDTE = dte;
  57. }
  58. /// <summary>
  59. /// Attaches the name of to process by.
  60. /// </summary>
  61. /// <param name="project">The project.</param>
  62. /// <param name="processName">Name of the process.</param>
  63. public void AttachToProcessByName(ISharePointProject project, String processName)
  64. {
  65. EnvDTE.Project dteProj = project.ProjectService.Convert<ISharePointProject, EnvDTE.Project>(project);
  66. project.ProjectService.Logger.ActivateOutputWindow();
  67. project.ProjectService.Logger.WriteLine(String.Format("Trying to attach to {0} process", processName), LogCategory.Message);
  68. if (AttachToProcessByName(processName))
  69. {
  70. project.ProjectService.Logger.WriteLine(String.Format("Debugger attached to {0} process", processName), LogCategory.Message);
  71. }
  72. else
  73. {
  74. project.ProjectService.Logger.WriteLine(String.Format("Failed to attach to {0} process", processName), LogCategory.Warning);
  75. }
  76. }
  77. /// <summary>
  78. /// Attaches the name of to process by.
  79. /// </summary>
  80. /// <param name="processName">Name of the process.</param>
  81. /// <returns></returns>
  82. public bool AttachToProcessByName(String processName)
  83. {
  84. Debugger2 debugger = CurrentDTE.Application.Debugger as Debugger2;
  85. if (debugger != null)
  86. {
  87. foreach (EnvDTE80.Process2 process in debugger.LocalProcesses)
  88. {
  89. if (process.Name.ToUpper().LastIndexOf(processName.ToUpper()) == (process.Name.Length - processName.Length))
  90. {
  91. process.Attach();
  92. }
  93. }
  94. return true;
  95. }
  96. return false;
  97. }
  98. /// <summary>
  99. /// Determines whether [is process available by name] [the specified process name].
  100. /// </summary>
  101. /// <param name="processName">Name of the process.</param>
  102. /// <returns>
  103. /// <c>true</c> if [is process available by name] [the specified process name]; otherwise, <c>false</c>.
  104. /// </returns>
  105. public bool IsProcessAvailableByName(String processName)
  106. {
  107. Debugger2 debugger = CurrentDTE.Application.Debugger as Debugger2;
  108. if (debugger != null)
  109. {
  110. foreach (EnvDTE80.Process2 process in debugger.LocalProcesses)
  111. {
  112. if (process.Name.ToUpper().LastIndexOf(processName.ToUpper()) == (process.Name.Length - processName.Length))
  113. {
  114. return true;
  115. }
  116. }
  117. }
  118. return false;
  119. }
  120. /// <summary>
  121. /// Restarts the process. No need to create an object with DTE.
  122. /// </summary>
  123. /// <param name="processName">Name of the process.</param>
  124. /// <param name="timeOut">The time out in seconds.</param>
  125. /// <returns></returns>
  126. public bool RestartProcess(String processName, int timeOut)
  127. {
  128. try
  129. {
  130. TimeSpan timeOutSpan = TimeSpan.FromSeconds(timeOut);
  131. ServiceController sc = new ServiceController(processName);
  132. if (sc.Status == ServiceControllerStatus.Running)
  133. {
  134. sc.Stop();
  135. sc.WaitForStatus(ServiceControllerStatus.Stopped, timeOutSpan);
  136. }
  137. sc.Start();
  138. sc.WaitForStatus(ServiceControllerStatus.Running, timeOutSpan);
  139. return true;
  140. }
  141. catch
  142. {
  143. throw;
  144. }
  145. }
  146. /// <summary>
  147. /// Executes the browser URL process.
  148. /// </summary>
  149. /// <param name="url">The URL.</param>
  150. public void ExecuteBrowserUrlProcess(string url)
  151. {
  152. try
  153. {
  154. Uri uri = new Uri(url);
  155. ExecuteBrowserUrlProcess(uri);
  156. }
  157. catch (UriFormatException)
  158. {
  159. }
  160. catch (ArgumentNullException)
  161. {
  162. }
  163. }
  164. /// <summary>
  165. /// Executes the browser URL process.
  166. /// </summary>
  167. /// <param name="uri">The URI.</param>
  168. public void ExecuteBrowserUrlProcess(Uri uri)
  169. {
  170. try
  171. {
  172. // Start Internet Explorer. Defaults to the home page.
  173. string browser = GetDefaultBrowser();
  174. if (!String.IsNullOrEmpty(browser))
  175. {
  176. System.Diagnostics.Process.Start(browser, uri.AbsoluteUri);
  177. }
  178. else
  179. {
  180. System.Diagnostics.Process.Start(CKSProperties.ProcessUtilities_IEProcessName, uri.AbsoluteUri);
  181. }
  182. }
  183. catch (UriFormatException)
  184. {
  185. }
  186. catch (ArgumentNullException)
  187. {
  188. }
  189. }
  190. /// <summary>
  191. /// Gets the default browser.
  192. /// </summary>
  193. /// <returns></returns>
  194. private string GetDefaultBrowser()
  195. {
  196. string browser = string.Empty;
  197. RegistryKey key = null;
  198. try
  199. {
  200. key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
  201. //trim off quotes
  202. browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
  203. if (!browser.EndsWith("exe"))
  204. {
  205. //get rid of everything after the ".exe"
  206. browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
  207. }
  208. }
  209. finally
  210. {
  211. if (key != null)
  212. {
  213. key.Close();
  214. }
  215. }
  216. return browser;
  217. }
  218. /// <summary>
  219. /// Recycles all application pools.
  220. /// </summary>
  221. /// <param name="service">The service.</param>
  222. public void RecycleAllApplicationPools(ISharePointProjectService service)
  223. {
  224. string[] names = GetAllApplicationPoolNames(service);
  225. foreach (string name in names)
  226. {
  227. RecycleApplicationPool(name);
  228. }
  229. }
  230. /// <summary>
  231. /// Recycles the application pool.
  232. /// </summary>
  233. /// <param name="service">The service.</param>
  234. /// <param name="url">The URL.</param>
  235. /// <returns></returns>
  236. public bool RecycleApplicationPool(ISharePointProjectService service, string url)
  237. {
  238. string name = GetApplicationPoolName(service, url);
  239. if (String.IsNullOrEmpty(name))
  240. {
  241. return false;
  242. }
  243. else
  244. {
  245. RecycleApplicationPool(name);
  246. return true;
  247. }
  248. }
  249. /// <summary>
  250. /// Recycles the application pool.
  251. /// </summary>
  252. /// <param name="name">The name.</param>
  253. public void RecycleApplicationPool(string name)
  254. {
  255. try
  256. {
  257. using (DirectoryEntry appPool = new DirectoryEntry(GetApplicationPoolPath(name)))
  258. {
  259. appPool.Invoke("Stop", null);
  260. appPool.Invoke("Start", null);
  261. }
  262. }
  263. catch (Exception) { }
  264. }
  265. /// <summary>
  266. /// Gets the application pool path.
  267. /// </summary>
  268. /// <param name="appPoolName">Name of the app pool.</param>
  269. /// <returns></returns>
  270. private string GetApplicationPoolPath(string appPoolName)
  271. {
  272. string path = "IIS://localhost/w3svc/apppools/" + appPoolName;
  273. return path;
  274. }
  275. /// <summary>
  276. /// Gets the name of the application pool.
  277. /// </summary>
  278. /// <param name="service">The service.</param>
  279. /// <param name="url">The URL.</param>
  280. /// <returns></returns>
  281. public string GetApplicationPoolName(ISharePointProjectService service, string url)
  282. {
  283. string name = service.SharePointConnection.ExecuteCommand<string, string>(DeploymentSharePointCommandIds.GetApplicationPoolName, url);
  284. return name;
  285. }
  286. /// <summary>
  287. /// Gets all application pool names.
  288. /// </summary>
  289. /// <param name="service">The service.</param>
  290. /// <returns></returns>
  291. public string[] GetAllApplicationPoolNames(ISharePointProjectService service)
  292. {
  293. string[] names = service.SharePointConnection.ExecuteCommand<string[]>(DeploymentSharePointCommandIds.GetAllApplicationPoolNames);
  294. return names;
  295. }
  296. /// <summary>
  297. /// Restarts the IIS.
  298. /// </summary>
  299. public void RestartIIS(ISharePointProject project)
  300. {
  301. StartProcess(project, System.Environment.SystemDirectory + Path.DirectorySeparatorChar + ProcessConstants.IISProcess, "localhost");
  302. }
  303. /// <summary>
  304. /// Starts the process.
  305. /// </summary>
  306. /// <param name="project">The project.</param>
  307. /// <param name="path">The path.</param>
  308. /// <param name="arguments">The URL.</param>
  309. public void StartProcess(ISharePointProject project,
  310. string path,
  311. string arguments)
  312. {
  313. ProcessStartInfo processStartInfo = new ProcessStartInfo(path,
  314. arguments);
  315. processStartInfo.RedirectStandardError = true;
  316. processStartInfo.RedirectStandardOutput = true;
  317. processStartInfo.UseShellExecute = false;
  318. ProcessStartInfo processStartInfo1 = processStartInfo;
  319. System.Diagnostics.Process process = System.Diagnostics.Process.Start(processStartInfo1);
  320. ThreadSafeStreamReader threadSafeStreamReader = new ThreadSafeStreamReader(process.StandardOutput);
  321. ThreadSafeStreamReader threadSafeStreamReader1 = new ThreadSafeStreamReader(process.StandardError);
  322. System.Threading.Thread thread = new System.Threading.Thread(new ThreadStart(threadSafeStreamReader.Go));
  323. System.Threading.Thread thread1 = new System.Threading.Thread(new ThreadStart(threadSafeStreamReader.Go));
  324. thread.Start();
  325. thread1.Start();
  326. thread.Join();
  327. thread1.Join();
  328. process.WaitForExit();
  329. if (!string.IsNullOrEmpty(threadSafeStreamReader1.Text))
  330. {
  331. project.ProjectService.Logger.WriteLine(threadSafeStreamReader1.Text, LogCategory.Error);
  332. project.ProjectService.Logger.WriteLine("Executing failed", LogCategory.Status);
  333. return;
  334. }
  335. else
  336. {
  337. project.ProjectService.Logger.WriteLine(threadSafeStreamReader.Text, LogCategory.Message);
  338. project.ProjectService.Logger.WriteLine("Installing assembly to GAC succeeded", LogCategory.Status);
  339. return;
  340. }
  341. }
  342. /// <summary>
  343. /// Copies to GAC.
  344. /// </summary>
  345. /// <param name="project">The project.</param>
  346. /// <param name="assemblyFullPath">The assembly full path.</param>
  347. public void CopyToGAC(ISharePointProject project,
  348. string assemblyFullPath)
  349. {
  350. //For more info on the GACUtil.exe http://msdn.microsoft.com/en-gb/library/ex0ss12c(v=vs.110).aspx
  351. StartProcess(project, "c:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.0A\\bin\\NETFX 4.0 Tools\\gacutil.exe",
  352. string.Format(@"/if ""{0}""", assemblyFullPath));
  353. }
  354. }
  355. }