Pass multiple parameters to powershell file through Process.Start - c#

I'm having an issue on passing multiple parameters for a powershell file to run through Process.Start. This is what I have so far
strCmdText = "/c powershell.exe -file " + filePath + " " + configs[i].name + " " + configs[i].path + "/" + configs[i].extension + " " + configs[i].value;
System.Diagnostics.Process pro = System.Diagnostics.Process.Start("cmd.exe", strCmdText);
I've found many examples that would demonstrate how to execute a powershell file through this but never any one that had multiple parameters, I assumed it'd be something like this but the command isn't working properly.

Related

How to create a desktop shortcut as a url file with parameters

I want to create a shortcut on the desktop to a app with c#. Here is my code:
StreamWriter sw = new StreamWriter(DesktopPath + #"\" + theData.Name + ".url");
sw.WriteLine("[InternetShortcut]");
sw.WriteLine("URL=file:///" + #"C:\Program Files\some Software\app.exe -r " + theData.Name + " -u " + theData.User);
sw.WriteLine("IconIndex=0");
sw.Close();
This works fine, accepting the parameters (-r, -u or other stuff). With parameters there appears a error that the file was not found and I should be sure about the spelling an try it again. Sorry the error message is not in English so I have to translate.
If I write it without parameters to the url file it works.
In the cmd the parameters works.
This may sound overly simplified; however, I would recommend that you give this a shot.
StreamWriter sw = new StreamWriter(DesktopPath + #"\" + theData.Name + ".url");
sw.WriteLine("[InternetShortcut]");
sw.WriteLine("URL=file:///" + #"""C:\Program Files\some Software\app.exe"" -r " + theData.Name + " -u " + theData.User);
sw.WriteLine("IconIndex=0");
sw.Close();
This is placing quotes around the application file path allowing a seperation of information from the arguments that you're passing.

FileNotFound exception when executing jar file

I'm creating a epubchecker gui using c#
there is a free epubchecker written in java.
its call epubcheck
to run using command line
java -jar epubcheck.jar file.epub
and my c# code is
System.Diagnostics.Process clientProcess = new Process();
clientProcess.StartInfo.FileName = "java";
clientProcess.StartInfo.Arguments = #" -jar " + #"C:\Users\User\Documents\Visual Studio 2015\Projects\epubcheck-4.0.1\epubcheck.jar" + " " + #"C:\Users\User\Desktop\v3.epub";
clientProcess.Start();
clientProcess.WaitForExit();
and the error is
The system cannot find the path specified
but i checked the location of the jar file and epub
i copied the string on my program and pasted the string on file explorer. and the file and folder exist.
so what am i doing wrong?
Add path to java to environment path variable.
Since you have spaces in your file path, you need to surround them in quotes:
clientProcess.StartInfo.Arguments = #" -jar " + #"""C:\Users\User\Documents\Visual Studio 2015\Projects\epubcheck-4.0.1\epubcheck.jar""" + " " + #"""C:\Users\User\Desktop\v3.epub""";
try this
clientProcess.StartInfo.Arguments = #" -jar " + #"C:\\Users\\User\\Documents\Visual Studio 2015\\Projects\\epubcheck-4.0.1\\epubcheck.jar" + " " + #"C:\\Users\\User\\Desktop\\v3.epub";

Use MSBuild to Update a Program

We use a MSBuild do create a Setup.exe. This Setup.exe will be uploaded on a Server and the programm use this file to reinstall the new Version of the program. My task is to speed up this process.
I would like to write a MSBuild which compiles the exe. So the customer don't need to download the whole 40mb but rather it updates his program.
Is that possible with MSBuild or is there another (better) solution.
I created patches.
I just want to give a short answer. It will probably be superficial.
So, everything is generated with a build.msbuild file.
Create folders with all the versions of the program (1.0.0.0, 1.0.1.0,…). Each folders also contains a .wixpdb file. This file is needed to generate the patches.
I created a msbuild-Task in C# to automate the patch generation. It looks like that:
…
foreach directory
…
if (!startTool("candle","\""+wurzel + "\\Setup\\MSI\\Patch.wxs\" -o \""+wurzel+"\\Setup\\MSI\\Patch.wixobj\""))
return false;
if (!startTool("light","\"" + wurzel + "\\Setup\\MSI\\Patch.wixobj\" -o \"" + wurzel + "\\Setup\\MSI\\Patch.wixmsp\""))
return false;
if (!startTool("torch", " -p -xi \"" + older.wixpdb + "\" \"" + newer.wixpdb + "\" -out \"" + wurzel + "\\Setup\\MSI\\Patch.wixmst\""))
return false;
if (!startTool("cmd", "/c cd /d \""+wurzel+"\\Setup\\MSI\" && pyro Patch.wixmsp -t MyPatch Patch.wixmst -out \"" + wurzel + "..\\Release\\Patches\\Patch_" + kvp.Value + "-" + versionNrS + ".msp\""))
return false;
…
The program automatically downloads the right patch and installs it with this command:
"msiexec /qb /p " + fd.Directory.FullName + "\\"+currentFtp.FileName + " REINSTALL=ALL REINSTALLMODE=a”

How do I append a jpg file to a rar file?

I recently saw this tutorial (please see before proceding). I tried it and it works fine.
I want to write a program that will accomplish the same result, using a GUI. see image-
Specifically, i want the c# equivalent of:
Copy \B SrcImage + SrcArchive Result
where SrcImage, SrcArchive and Result are strings pointing to three files.
Does anyone know what I can do?
UPDATE #1:
I tried this:
//Writing a batch file containing the desired code in batch file
//language, and running it from a c# form.
string BackSlashB = "-b";
string lines = "#echo off \r copy "+ BackSlashB +" " + SrcJpg + " + " + SrcRar + " " + Destfile + " \r pause";
MessageBox.Show(lines);
System.IO.StreamWriter batchfile = new System.IO.StreamWriter("c:\\temp.bat");
batchfile.WriteLine(lines);
batchfile.Close();
System.Diagnostics.Process.Start("c:\\temp.bat");
A console window opens up and then disappears before i can read it's contents...
Any ideas?
Alright, I finally got it to work.
The problem was with the \r escape. for some odd reason, it has to be \r\n, or else the batch file will contain the code in one line.
Also, I had to add apostrophes around each file name.
Finally, i removed the pause and debug box, replacing them with a "check if file exists" in order to give the user a message that the task was completed.
Entire Code:
string lines = "#echo off" + "\r\n copy /b " + "\"" + SrcJpg + "\"" + " + " + "\"" + SrcRar + "\"" + " " + "\"" + Destfile + "\"";
//MessageBox.Show(lines);
System.IO.StreamWriter batchfile = new System.IO.StreamWriter("c:\\temp.bat");
batchfile.WriteLine(lines);
batchfile.Close();
System.Diagnostics.Process.Start("c:\\temp.bat");
if (File.Exists(Destfile))
{
MessageBox.Show("File Created!");
}

Access is denied (Running a .bat file)

This is my code to build up a .bat file and run it.
StringBuilder Batch = new StringBuilder();
Batch.AppendLine("#echo off");
Batch.AppendLine("taskkill /IM " + Process.GetCurrentProcess().ProcessName + ".exe /F");
Batch.AppendLine("ping localhost > nul");
Batch.AppendLine("del /f " + (char)34 + Application.ExecutablePath + (char)34);
Batch.AppendLine("ren " + (char)34 + Application.StartupPath + #"\update.exe" + (char)34 + " " + Process.GetCurrentProcess().ProcessName);
Batch.AppendLine((char)34 + Application.ExecutablePath + (char)34);
Batch.AppendLine("del %0");
File.WriteAllText(Application.StartupPath + #"\update.bat", Batch.ToString(), Encoding.Default);
Process.Start(Application.StartupPath + #"\update.bat");
However, I get access is denied, I have to run it as a admin, how can I do it?
Not an answer to your question, but I think you are not approaching it from the best direction. Instead see this question that provides a very robust answer for what you are actually trying to do: what-is-the-best-way-to-auto-update-a-windows-application
TL;DR If your original installation is "All users" then you will have problems with having to manually elevate permissions. Is the installation is "Per user" then this simplifies the permissions issue.

Categories