Sample of a Manager with Singleton to execute command on windows shell
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace Framework
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Manager shell. </summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
public class ManagerShell
{
#region Gestion du singleton
/// <summary> The instance. </summary>
private static ManagerShell instance = null;
/// <summary> The padlock. </summary>
private static readonly object padlock = new object();
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Prevents a default instance of the <see cref="ManagerShell"/> class from being created.
/// </summary>
////////////////////////////////////////////////////////////////////////////////////////////////////
ManagerShell()
{
//if (_Log.IsDebugEnabled)
// _Log.DebugFormat("New instance of {0}", Assembly.GetExecutingAssembly());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Gets the instance. </summary>
///
/// <value> The instance. </value>
////////////////////////////////////////////////////////////////////////////////////////////////////
public static ManagerShell Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new ManagerShell();
}
return instance;
}
}
}
#endregion Gestion du singleton
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Invokes the prompt. (silently) </summary>
///
/// <param name="Command"> The command. </param>
///
/// <returns> true if it succeeds, false if it fails. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool InvokePrompt(string command)
{
if (String.IsNullOrEmpty(command))
throw new ArgumentNullException("command");
try
{
Process invokePrompt = new Process();
invokePrompt.StartInfo.FileName = Environment.GetEnvironmentVariable("COMSPEC");
invokePrompt.StartInfo.Arguments = "/Q /K /C " + command;
invokePrompt.StartInfo.UseShellExecute = false;
invokePrompt.StartInfo.RedirectStandardOutput = false;
invokePrompt.StartInfo.CreateNoWindow = true;
invokePrompt.Start();
invokePrompt.WaitForExit();
invokePrompt.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary> Invokes the process. (silently) </summary>
///
///
/// <param name="ProcessName"> Name of the process. </param>
/// <param name="Command"> The command. </param>
///
/// <returns> true if it succeeds, false if it fails. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool InvokeProcess(string processName, string command)
{
if (String.IsNullOrEmpty(processName))
throw new ArgumentNullException("processName");
if (String.IsNullOrEmpty(command))
throw new ArgumentNullException("command");
try
{
Process invokeProcess = new Process();
invokeProcess.StartInfo.FileName = processName;
invokeProcess.StartInfo.Arguments = command;
invokeProcess.StartInfo.UseShellExecute = false;
invokeProcess.StartInfo.RedirectStandardOutput = false;
invokeProcess.StartInfo.CreateNoWindow = true;
invokeProcess.Start();
invokeProcess.WaitForExit();
invokeProcess.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}