Complete C# Program
-------------------
using System;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Remoting;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace Target_Process
{
class Program
{
static void Main(string[] args)
{
try
{
CreateProcessWithLoadProfile("Password","Command or Application Path","Arguments");
}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex);
}
}
private static void CreateProcessWithLoadProfile(
string password,
string commandPath,
string commandArguments)
{
Process MyProcess = new Process();
MyProcess.StartInfo.FileName = "cmd.exe";
MyProcess.StartInfo.Arguments = " /c " + commandPath + " " + commandArguments;
MyProcess.StartInfo.UseShellExecute = false;
MyProcess.StartInfo.CreateNoWindow = true;
MyProcess.StartInfo.LoadUserProfile = true;
MyProcess.StartInfo.RedirectStandardOutput = true;
MyProcess.StartInfo.RedirectStandardError = true;
MyProcess.StartInfo.RedirectStandardInput = true;
// Set our event handler to asynchronously read the sort output.
MyProcess.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
MyProcess.ErrorDataReceived += new DataReceivedEventHandler(ProcessErrorHandler);
// get the domain and user name parts of the current
// windows identity
Match identity_match = Regex.Match(
WindowsIdentity.GetCurrent().Name,
@"^([^\\]+)\\(.+)$");
// domain name
string dn = identity_match.Groups[1].Value;
// user name
string un = identity_match.Groups[2].Value;
// only set the domain if it is an actual domain and
// not the name of the local machine, i.e. a local account
// invoking sudo
if (!Regex.IsMatch(dn,
Environment.MachineName, RegexOptions.IgnoreCase))
{
MyProcess.StartInfo.Domain = dn;
}
MyProcess.StartInfo.UserName = un;
// transform the plain-text password into a
// SecureString so that the ProcessStartInfo class
// can use it
MyProcess.StartInfo.Password = new System.Security.SecureString();
for (int x = 0; x < password.Length; ++x)
MyProcess.StartInfo.Password.AppendChar(password[x]);
try
{
MyProcess.Start();
}
catch (Exception e)
{
throw new Exception(null, e);
}
// Start the asynchronous read of the sort output stream.
MyProcess.BeginOutputReadLine();
MyProcess.BeginErrorReadLine();
MyProcess.WaitForExit();
MyProcess.Close();
}
private static void ProcessOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
Console.WriteLine(outLine.Data);
}
private static void ProcessErrorHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
Console.WriteLine(outLine.Data);
}
}
}
Regards,
Megha