public string GetEnvironmentVars()
{
Process process = new Process();
// UseShellExecute must be 'false' when redirecting the output
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "p4";
process.StartInfo.Arguments = " set ";
//process.StartInfo.WorkingDirectory = dir;
process.Start();
string output = process.StandardOutput.ReadToEnd();
//string error = process.StandardError.ReadToEnd();
// Wait a reasonable ammount of time for it to finish
bool res = process.WaitForExit(5000);
return res ? output : "Command \"" +
process.StartInfo.FileName + process.StartInfo.Arguments +
"\" failed to exit:" + Environment.NewLine + output;
}
Showing posts with label process. Show all posts
Showing posts with label process. Show all posts
July 1, 2010
Start a Process and Capture the Output in c#
Simple sample for running a process synchronously and capturing the output
Labels:
c#,
process,
RedirectStandardOutput
June 29, 2010
Open An EMail In Outlook
If are running a PC with Outlook and you want to open an email using Outlook from your application (but not send it immediately)
Following sample opens an email dialog with a specified document already attached
Following sample opens an email dialog with a specified document already attached
private void butEMailFiles_Click(object sender, RoutedEventArgs e)
{
string archiveName = GetExistingArchiveName();
if (File.Exists(archiveName))
{
RunShellCommand("Outlook.exe", "/a \"" + archiveName + "\"");
}
}
private static void RunShellCommand(string app, string args)
{
string dir = System.IO.Path.GetDirectoryName(app);
string file = System.IO.Path.GetFileName(app);
Process process = new Process();
process.StartInfo.FileName = file;
process.StartInfo.WorkingDirectory = dir;
process.StartInfo.Arguments = args;
process.Start();
}
Run Batch File in C#
Quick method to create and run a batch file, the contents of which are passed as a string.
private void RunBatchFile(
string batFileContents,
int waitToFinishSecs)
{
string batFile = Path.GetTempFileName();
batFile = batFile.Replace(".tmp", ".bat");
File.WriteAllText(batFile, batFileContents);
string dir = System.IO.Path.GetDirectoryName(batFile);
string file = System.IO.Path.GetFileName(batFile);
Process process = new Process();
process.StartInfo.FileName = file;
process.StartInfo.WorkingDirectory = dir;
bool res = false; // Dont do anything with this yet
try
{
process.Start();
res = process.WaitForExit(waitToFinishSecs*1000);
File.Delete(batFile);
}
catch (Win32Exception winex)
{
Debug.WriteLine(winex.ToString());
throw;
}
}
May 20, 2010
Open Explorer At Some Directory in C#
Use the following code snippet:
// Open explorer in user temporary directory
OpenExplorerInDir(System.IO.Path.GetTempPath());
...
private void OpenExplorerInDir(string dir)
{
string exe = "explorer.exe";
//Driectory.Exists
Process exeProcess = new Process();
exeProcess.StartInfo.FileName = exe;
exeProcess.StartInfo.Arguments = "/e,/root," + dir;
exeProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
exeProcess.Start();
}
December 14, 2008
Using Process
// Starting a windows program with Process
private void StartXXX()
{
string dir = @"C:\CODE\blahblahblah\Server\XXX\bin";
string exe = "XXX.exe";
Process shellRunProcess = new Process();
shellRunProcess.StartInfo.FileName = exe;
shellRunProcess.StartInfo.WorkingDirectory = dir;
shellRunProcess.StartInfo.Arguments = "-c";
shellRunProcess.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
shellRunProcess.Start();
}
// Terminating a process
private void TerminateXXX()
{
// Remove the '.exe' extension when looking for the process
Process[] processes = Process.GetProcessesByName("XXX");
foreach (Process p in processes)
{
p.CloseMainWindow();
}
}
When running a batch file or console type application, you can start if like so and
have the output redirected to a control in your form. See this link for more information
Launching A Console Process And Redirecting the Output
private void StartProcessWithoutWindow()
{
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = FileName;
process.StartInfo.Arguments = Arguments;
process.StartInfo.WorkingDirectory = WorkingDirectory;
process.Start();
}
August 5, 2008
Using Shell To Start A Process
Starting another OS process. Eg to open an image in the defult editor/viewer
Process shellRunProcess = new Process(); shellRunProcess.StartInfo.FileName = fileName; shellRunProcess.Start();
Subscribe to:
Posts (Atom)