C#: Executing an EXE file - c#

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

Related

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.

Start a process with a command line parameter of another executable

I'm trying to pass an .exe to another through C# code.
Here's my code so far:
string ex1 = System.Windows.Forms.Application.StartupPath.ToString() + "\\dev\\psm.exe";
string ex2 = System.Windows.Forms.Application.StartupPath.ToString() + "\\dev\\Application\\app.exe";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = ex1;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.Arguments = ex2;
try
{
Process.Start(startInfo);
}
catch
{
}
Which argument would work as dragging a file onto the application?
Details:
When you run psm.exe normally, it prompts for file name and directory.
However, when you drag an approved app on psm.exe,
it loads the app automatically.
How can this be done with C#?
You can run another app synchronously like this:
System.Diagnostics.Process myapp = new System.Diagnostics.Process();
myapp.StartInfo.FileName = ex1;
myapp.StartInfo.Arguments = ex2;
myapp.Start();
myapp.WaitForExit();
Depending on how the app you want to launch expects the command line arguments to be passed, you may need this for the arguments:
myapp.StartInfo.Arguments = String.Format("/MyArgumentName={0}", ex2);
That would be the equivalent of:
c:\MyApplicationStartupPath\dev\psm.exe /MyArgumentName=c:\MyApplicationStartupPath\Application\App.exe
Be sure to match the way app.exe expects the parameters in your StartInfo.Arguments

Call Winzip32.exe with parameter with c#

I want zip folder through my console application that's why I used something like
public void DoWinzip(string zipName, string password, string folderName)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files\\WinZip\\winzip32.exe";
startInfo.Arguments = string.Format("-min -eZ {0} {1}", zipName, folderName);
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(Exception ex)
{
// Log error.
}
}
But this will give me error like winzip parameters validation error. Where I do mistake?
Update
I spell wrong on -eZ actually it may -ex etc... But another problem is that winzip open up own windows. I write for it -min however it opened.
Perhaps you are passing paths with whitespaces (in zipName and folderName arguments) without enclosing them in double quotes.
You can avoid to open up windows by using ProcessStartInfo.WindowStyle property
Try this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files\\WinZip\\winzip32.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
http://www.rondebruin.nl/parameters.htm -> looking at that I would think the code is:
startInfo.Arguments = string.Format("-e {0} {1}", zipName, folderName);
What is the option -eZ? I think that is your issue
I thought that that following are the only options to determine the compression method.
-ex = eXtra
-en = Normal
-ef = Fast
-es = Super fast
-e0 = no compression

Executing command-prompt command in C#

:)
I have a software which can be executed via command line, and now I want it to be executed directly from my C# app. Sadly, there is no error but I still can't do it. :(
The path of .exe file of the software is C:\program files\mysoftware.exe
The command I would like to input is
cd c:\program files\mysoftwareFolder
enter
mysoftware.exe d:\myfolder\file1.xxx d:\myfolder\file2.xxx -mycommand
enter
exit
The commands above work so well in the actual command prompt, but they just don't work from my C# code.
Here is the code:
Process cmdprocess = new Process();
System.Diagnostics.ProcessStartInfo startinfo = new System.Diagnostics.ProcessStartInfo();
startinfo.FileName = "cmd";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
System.IO.StreamReader sr = cmdprocess.StandardOutput;
System.IO.StreamWriter sw = cmdprocess.StandardInput;
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine(#"cd" +#"program files\mysoftwarefolder");
sw.WriteLine(#"mysoftware.exe" +#"d:\myfolder\file1.xxx" +#"d:\myfolder\file2.xxx" +#"-mycommand");
sw.WriteLine(#"exit");
sw.Close();
sr.Close();
I guess the incorrect parts might be "startinfo.FileName = "cmd";" or the way I typed the command in the code, but I have no idea how to correct them. :(
Please tell me what I did wrong. I appreciate every answer from you! :)))
UPDATE Thank you for your helps! I tried writing the command in batch file, but it only works in debugging mode. (I forgot to tell you guys that I am developing a web service.) When I run my external project which will use this C# service, it won't work. I don't know whether I should add something to my code or not.
help meeeeee pleaseeeee (T___T)
Write these commands in a batch file and execute the batch file.
In batch file:
cd c:\program files\mysoftwareFolder
mysoftware.exe
d:\myfolder\file1.xxx
d:\myfolder\file2.xxx -mycommand
exit
Code:
Process cmdprocess = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "path to batchfile.bat";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
Instead of:
startinfo.FileName = "cmd";
Directly use
startinfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
Then pass the arguments to the start info as
startinfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
So the whole code looks like:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = #"c:\program files\mysoftwarefolder\mysoftware.exe";
p.StartInfo.Arguments = #"d:\myfolder\file1.xxx " +#"d:\myfolder\file2.xxx " +#"-mycommand";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
If you need to see output from your program you can simply use the output string.
2 things: I think you have spacing problems and you're not reading the result of these commands. cmd is probably telling you ..."is not recognized as an internal or external command"
If you look at what you're throwing at cmd, it will be:
echo on
c:
cdprogram files\mysoftware folder
mysoftware.exed:\myfolder\file1.xxx
That won't work when you try it in cmd. CMD is almost certainly kicking back error messages at you, but you're never reading from sr so you'll never know it.
I'd add in some spaces and include all the paths in quotes internally like so:
sw.WriteLine(#"echo on");
sw.WriteLine(#"c:");
sw.WriteLine("cd \"program files\\mysoftwarefolder\"");
sw.WriteLine("mysoftware.exe \"d:\\myfolder\\file1.xxx\" d:\\myfolder\\file2.xxx\" -mycommand");
sw.WriteLine(#"exit");

How to trap an error from a shelled process in 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;

Categories

Resources