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.
Related
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.
I have a problem, I've written a code to install a batch file to the registry and to the PATH under Advanced Settings, so it can be used under the right click menu.
It works until I click on this right click item. After that it just shows a window for a millisecond (I don't know which one) and nothings happens.
The batch file is working (I tested it). I also checked the registry entry (it is 100% correct, like it should be when I do it manually). So the only thing is left is the PATH under Advanced System Settings...
First the code:
Environment.SetEnvironmentVariable(
"PATH",
Environment.GetEnvironmentVariable("PATH") + ";" +
Path.GetPathRoot(Environment.SystemDirectory) + "FastAndDelete\\",
EnvironmentVariableTarget.Machine);**
RegistryKey reg = Registry
.ClassesRoot.OpenSubKey("Directory", true)
.OpenSubKey("shell", true);
reg.CreateSubKey("FastAndDelete");
reg.OpenSubKey("FastAndDelete", true)
.CreateSubKey("command")
.SetValue("", "cmd /c " + '“' + "cd %1 && fastdel.bat" + '”');
reg.Close();
I also tried
Environment.SetEnvironmentVariable(
"PATH",
Environment.GetEnvironmentVariable("PATH") + ";" +
Path.GetPathRoot(Environment.SystemDirectory) +
"FastAndDelete\\;",
EnvironmentVariableTarget.Machine);
and
Environment.SetEnvironmentVariable(
"PATH",
Environment.GetEnvironmentVariable("PATH") + ";" +
Path.GetPathRoot(Environment.SystemDirectory) +
"FastAndDelete\\fastdel.bat",
EnvironmentVariableTarget.Machine);
and
Environment.SetEnvironmentVariable(
"PATH",
Environment.GetEnvironmentVariable("PATH") + ";" +
Path.GetPathRoot(Environment.SystemDirectory) +
"FastAndDelete\\fastdel.bat;",
EnvironmentVariableTarget.Machine);
I also tried it without the ";" at the beginning, but nothing helps.
Do someone know what I am doing wrong?
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.
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”
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!");
}