Sample of a Manager with Singleton to execute command on windows shell


SUBMITTED BY: Guest

DATE: Dec. 7, 2013, 4:23 p.m.

FORMAT: Text only

SIZE: 5.3 kB

HITS: 53799

  1. Sample of a Manager with Singleton to execute command on windows shell
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Diagnostics;
  7. namespace Framework
  8. {
  9. ////////////////////////////////////////////////////////////////////////////////////////////////////
  10. /// <summary> Manager shell. </summary>
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. public class ManagerShell
  13. {
  14. #region Gestion du singleton
  15. /// <summary> The instance. </summary>
  16. private static ManagerShell instance = null;
  17. /// <summary> The padlock. </summary>
  18. private static readonly object padlock = new object();
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. /// <summary>
  21. /// Prevents a default instance of the <see cref="ManagerShell"/> class from being created.
  22. /// </summary>
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////
  24. ManagerShell()
  25. {
  26. //if (_Log.IsDebugEnabled)
  27. // _Log.DebugFormat("New instance of {0}", Assembly.GetExecutingAssembly());
  28. }
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////
  30. /// <summary> Gets the instance. </summary>
  31. ///
  32. /// <value> The instance. </value>
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////
  34. public static ManagerShell Instance
  35. {
  36. get
  37. {
  38. lock (padlock)
  39. {
  40. if (instance == null)
  41. {
  42. instance = new ManagerShell();
  43. }
  44. return instance;
  45. }
  46. }
  47. }
  48. #endregion Gestion du singleton
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////
  50. /// <summary> Invokes the prompt. (silently) </summary>
  51. ///
  52. /// <param name="Command"> The command. </param>
  53. ///
  54. /// <returns> true if it succeeds, false if it fails. </returns>
  55. ////////////////////////////////////////////////////////////////////////////////////////////////////
  56. public bool InvokePrompt(string command)
  57. {
  58. if (String.IsNullOrEmpty(command))
  59. throw new ArgumentNullException("command");
  60. try
  61. {
  62. Process invokePrompt = new Process();
  63. invokePrompt.StartInfo.FileName = Environment.GetEnvironmentVariable("COMSPEC");
  64. invokePrompt.StartInfo.Arguments = "/Q /K /C " + command;
  65. invokePrompt.StartInfo.UseShellExecute = false;
  66. invokePrompt.StartInfo.RedirectStandardOutput = false;
  67. invokePrompt.StartInfo.CreateNoWindow = true;
  68. invokePrompt.Start();
  69. invokePrompt.WaitForExit();
  70. invokePrompt.Close();
  71. return true;
  72. }
  73. catch (Exception ex)
  74. {
  75. return false;
  76. }
  77. }
  78. ////////////////////////////////////////////////////////////////////////////////////////////////////
  79. /// <summary> Invokes the process. (silently) </summary>
  80. ///
  81. ///
  82. /// <param name="ProcessName"> Name of the process. </param>
  83. /// <param name="Command"> The command. </param>
  84. ///
  85. /// <returns> true if it succeeds, false if it fails. </returns>
  86. ////////////////////////////////////////////////////////////////////////////////////////////////////
  87. public bool InvokeProcess(string processName, string command)
  88. {
  89. if (String.IsNullOrEmpty(processName))
  90. throw new ArgumentNullException("processName");
  91. if (String.IsNullOrEmpty(command))
  92. throw new ArgumentNullException("command");
  93. try
  94. {
  95. Process invokeProcess = new Process();
  96. invokeProcess.StartInfo.FileName = processName;
  97. invokeProcess.StartInfo.Arguments = command;
  98. invokeProcess.StartInfo.UseShellExecute = false;
  99. invokeProcess.StartInfo.RedirectStandardOutput = false;
  100. invokeProcess.StartInfo.CreateNoWindow = true;
  101. invokeProcess.Start();
  102. invokeProcess.WaitForExit();
  103. invokeProcess.Close();
  104. return true;
  105. }
  106. catch (Exception ex)
  107. {
  108. return false;
  109. }
  110. }
  111. }
  112. }

comments powered by Disqus