How to trap an error from a shelled process in c# - c#

ive been using this site for a while and have my first question to ask - as the help here is awesome.
Im writing a c# dll (for use in VbScript) and in that DLDL im shelling out a command and wish to trap the return code of that command. So far I have the following c# code:-
public int InstallHotfix(String strAppName,string strExe,string strParam)
{
ProcessStartInfo startInfo = new ProcessStartInfo(strExe,strParam);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
I am calling this from a VB script but would like to know how to return the result back to VBS. I assume there a 'return' value that I have to enter but im unsure how to achieve this.
Cheers.

ProcessStartInfo startInfo = new ProcessStartInfo(strExe, strParam);
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process proc = Process.Start(startInfo);
proc.WaitForExit();
int exitCode = proc.ExitCode;

Related

ProcessStartInfo hangs when UseShellExecute = true

I was running some exe with ProcessStartInfo
ProcessStartInfo startInfo = new ProcessStartInfo("my.exe");
startInfo.Arguments = "foo.txt bar.txt";
startInfo.WorkingDirectory = path;
Process process = Process.Start(startInfo);
process.WaitForExit();
It was working fine on my PC, but when I move the code to another PC, the same code keep hanging.
After some trying, I was able to fix it by setting UseShellExecute to false (and use absolute path for the exe filename).
ProcessStartInfo startInfo = new ProcessStartInfo(Path.Combine(Server.MapPath(#"~\mypath"),"my.exe"));
startInfo.Arguments = "foo.txt bar.txt";
startInfo.UseShellExecute = false;
Process process = Process.Start(startInfo);
process.WaitForExit();
The question is why? Why does using shell hang the process? What is the difference between using or not using shell to execute?
Thanks for the help!

C#: Executing an EXE file

I read other answers to this, but cannot work mine. maybe because of the difference.
The difference is that my hex file are on an external device and here is the hierarchy
/home/root/myfolder/General
and the EXE that I want to execute
/TestLiberatus_Stress.exe
so here is what I have up to now
string ex1 = "/home/root/myfolder/Functional/TestLiberatus_Stress.exe";
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = ex1;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "";
try
{
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
int x = 0; //just to know it enters for now
}
From the other explanations that I've found, it should work... So, where is the problem?
Also once this work, what I'd like to do intead of giving the full path would be to just go up one level and go in stress. The executing EXE will be in the path I've given, the full path, and the other one to execute will be in
/home/root/myfolder/Functional

Calling specific Python Script from C# process

I'm trying to call python scripts from c#, so far so good. But when I try to call this specific script it is not workin. This is what I'm doing in C#:
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "C:\\Python27\\python.exe";
startInfo.Arguments = "pyScript.py";
// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (Process exeProcess = Process.Start(startInfo))
{
StreamReader output_sr = exeProcess.StandardOutput;
StreamReader error_sr = exeProcess.StandardError;
String output = output_sr.ReadToEnd();
String error = error_sr.ReadToEnd();
Console.WriteLine(output);
Console.WriteLine(error);
exeProcess.WaitForExit();
}
My pyScript.py file is the following:
with open ('test.txt','w') as f:
for i in range(0x34):
f.write('1')
f.close()
So basically what I spect is to create a file named 'test.txt' containing:
1111111111111111111111111111111111111111111111111111
Also I'm not getting any error, and if I run it from console it works, even if I double click my python script it works. This is really weird and I don't have a clue why when I call if from c# the file is not being generated.
Your 'test.txt' file is created in your Debug folder. Or simply where your C# executable is called from. Thus, it is working but you are looking wrong place.

Starting IE process as a current user working this way but not the other way

I want to open IE (it doesn't matter the web site it will open with), but I found out that using this code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = "http://www.google.com";
proc.Start();
will start the browser as a current user, but with this code:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files\\Internet Explorer\\iexplore.exe";
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
it wouldn't, why is that? what is the reason?
Wrap the code in a try catch and take a look at the exception being raised, that should help find out why it is not working.

Mimic batch file with C#

I have a batch file that runs the four commands
vsinstr -coverage hello.exe
vsperfcmd /start:coverage /output:run.coverage
hello
vsperfcmd /shutdown
How can I use C# to run the four commands?
Add these command to a batch file and use the below code to run it
ProcessStartInfo startInfo;
System.Diagnostics.Process batchExecute;
startInfo = new ProcessStartInfo("batchFilePath");
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
batchExecute = new System.Diagnostics.Process();
batchExecute.StartInfo = startInfo;
batchExecute.Start();
batchExecute.WaitForExit();
Run the commands using Process.Start.
Example
Using the override Process.Start(string fileName, string arguments)
Process.Start("vsinstr", "-coverage hello.exe");
Process.Start("vsperfcmd", "/start:coverage /output:run.coverage");
Process.Start("hello");
Process.Start("vsperfcmd", "/shutdown");
Since you already have a batch file, why not run it from C# instead of running the commands in it from C#? For example:Process.Start

Categories

Resources