Create / run batch file programmatically

I am trying to create a cmd file that will install .msi and then execute this cmd file using C # code. The code works fine if I run it using f5 or control + f5 from visual studio.

However, as soon as I pack the code in the .msi file and install it (this is a wpf application), when it executes the corresponding code, it opens the command window but does not execute the command. Instead, he says: “C: \ Program is not recognized as an internal or external command ...” I know that this error means that there are no quotes around the file path, but this is not the case as you can see below the quotes here. In addition, if I go to the program files directory, the Reinstall.cmd file is present and has quotation marks in it. I can even double-click the generated Reinstall.cmd file and it will execute just fine. What am I missing here?

Here is the code:

private string MSIFilePath = Path.Combine(Environment.CurrentDirectory, "HoustersCrawler.msi"); private string CmdFilePath = Path.Combine(Environment.CurrentDirectory, "Reinstall.cmd"); private void CreateCmdFile() { //check if file exists. if (File.Exists(CmdFilePath)) File.Delete(CmdFilePath); //create new file. var fi = new FileInfo(CmdFilePath); var fileStream = fi.Create(); fileStream.Close(); //write commands to file. using (TextWriter writer = new StreamWriter(CmdFilePath)) { writer.WriteLine(String.Format("msiexec /i \"{0}\"", MSIFilePath));// /quiet } } private void RunCmdFile() {//run command file to reinstall app. var p = new Process(); p.StartInfo = new ProcessStartInfo("cmd.exe", "/k " + CmdFilePath); p.Start(); p.WaitForExit(); } 
+4
source share
1 answer

as you can see below, there are quotes.

You put them in the first part, but not where you execute cmd.exe .

Since your path contains spaces, you need to wrap it in quotation marks. Try:

 p.StartInfo = new ProcessStartInfo("cmd.exe", "/k \"" + CmdFilePath + "\""); 
+3
source

Source: https://habr.com/ru/post/1338599/


All Articles