SEB picasso

SEB picasso

  • NA
  • 5
  • 5.7k

running external process in C#

Feb 18 2015 11:08 PM

I have a piece of NET 4.0 C# code that does not seem to work

The code is just supposed to extract the contents of an MSI. The following code compiles w/o errors but when running the application, an error displays “file specified cannot be found”

Process process = new Process();

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

startInfo.FileName = "msiexec ";

startInfo.Arguments = "/a \""+ textBox1.Text + "\" /qb TARGETDIR=\""+textBox1.Text +".EXT\"";

 

//output of message box looks like this, which is right syntax but application gets an error.

 from messageBox =  msiexec /a "C:\Temp\MyApplication.msi" /qb TARGETDIR="c:\temp\myapplication.msi.EXT"

 

The application still complaints that file cannot be found.

If a copy and paste the output of the messagebox into a batch file and then run the file, it works fine.  why is the code above not working?

 

I can also create the file and run it as follows.

if (File.Exists("C:\\temp\\_321.bat"))

File.Delete("C:\\temp\\_321.bat");

FileStream logFile = new FileStream("C:\\Temp\\_321.bat", FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter sw = new StreamWriter(logFile);

Process process = new Process();

ProcessStartInfo startInfo = new ProcessStartInfo();

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

startInfo.FileName = "msiexec ";

sw.Write(sw.NewLine);

 startInfo.Arguments = "/a \"" + textBox1.Text + "\" /qb TARGETDIR=\""+ textBox1.Text +".EXT\"";

sw.Write(startInfo.FileName+startInfo.Arguments);

sw.Close();

logFile.Close();

 

System.Diagnostics.Process.Start("c:\\temp\\_321.bat");

 

 

this also works fine.   Can someone advice as to what am I doing wrong in the first code. I think is cleaner and don’t want to use a batch file. Many thanks


Answers (2)