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
{
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Manager shell.
////////////////////////////////////////////////////////////////////////////////////////////////////
public class ManagerShell
{
#region Gestion du singleton
/// The instance.
private static ManagerShell instance = null;
/// The padlock.
private static readonly object padlock = new object();
////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// Prevents a default instance of the class from being created.
///
////////////////////////////////////////////////////////////////////////////////////////////////////
ManagerShell()
{
//if (_Log.IsDebugEnabled)
// _Log.DebugFormat("New instance of {0}", Assembly.GetExecutingAssembly());
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Gets the instance.
///
/// The instance.
////////////////////////////////////////////////////////////////////////////////////////////////////
public static ManagerShell Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new ManagerShell();
}
return instance;
}
}
}
#endregion Gestion du singleton
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Invokes the prompt. (silently)
///
/// The command.
///
/// true if it succeeds, false if it fails.
////////////////////////////////////////////////////////////////////////////////////////////////////
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;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Invokes the process. (silently)
///
///
/// Name of the process.
/// The command.
///
/// true if it succeeds, false if it fails.
////////////////////////////////////////////////////////////////////////////////////////////////////
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;
}
}
}
}