I'm trying to make an updater in C# and I want to delete the updater once it's done.
So I have this code in C#:
var path = Environment.CurrentDirectory + "\\WindowsFormsApplication1.exe";
Process.Start("cmd.exe /c del " + path);
But I get this error message:
Win32Exception was unhandled
The system cannot find the file specified
But I'm sure the path is spelled right, so I don't think that's the problem.
Any ideas?
var path = Environment.CurrentDirectory + "\\WindowsFormsApplication1.exe";
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = string.Format("/c del \"{0}\"", path);
process.Start();
Or
Process.Start("cmd.exe", string.Format("/c del \"{0}", path));
This question was answered here: Run Command Prompt Commands
Aditional solution is to build a BAT file and inside it to call del.
Related
I want cmd command in my c# winforms program. I want use command to compress file with 7zip.
In normal cmd i use cd "C:\Program Files(x86)\7-Zip" & 7z.exe a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
string zip = #"/c C:\Program Files(x86)\7-Zip";
string file = #"/c C:\xyz\txt";
string conv = "/c & 7z.exe a -tzip";
string method = "/c -m0=BZip2 -mx5";
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = #"C:\windows\system32\cmd.exe";
proc.Arguments = #"/c cd" + zip + conv + file + file + method;
System.Diagnostics.Process.Start(proc);
Doesn't work my code. How can i use this command in my program. I want compress file when i click in button
Something like this:
// File (exe) to start: combination of folder and exe
string fileName = Path.Combine(
// Let's not hardcode "C:\Program Files(x86)" and alike
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86),
#"7-Zip",
#"7z.exe");
// If desired arguments are
// a -tzip "C:\xyz\test\txt" "C:\xyz\test\txt" -m0=BZip2 -mx5
// we can join them as
string arguments = string.Join(" ",
#"a",
#"-tzip", //TODO: you may want to have these values
#"""C:\xyz\test\txt""", // as variables like file, conv, method etc.
#"""C:\xyz\test\txt""",
#"-m0=BZip2",
#"-mx5");
ProcessStartInfo procInfo = new ProcessStartInfo() {
FileName = fileName, // We want to start fileName
Arguments = arguments, // With arguments
}
// Process is IDisposable, do not forget to Dispose HProcess handle
using (var process = Process.Start(procInfo)) {
// It's fire and forget implementation, if you want to wait for results
// add process.WaitForExit();
// process.WaitForExit(); // uncomment, if you want to pause
}
You seem to have made a mistake with your arguments.
Your current command line will be:
C:\windows\system32\cmd.exe /c cd/c C:\Program Files(x86)\7-Zip/c & 7z.exe a -tzip/c C:\xyz\txt/c C:\xyz\txt/c -m0=BZip2 -mx5
The spurious /c everywhere is not going to help and neither are the lack of spaces.
I'm guessing what you meant to run was these two commands in succession:
cd C:\Program Files(x86)\7-Zip
7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5
Which means you should change your code to:
string zip = #" C:\Program Files(x86)\7-Zip";
string file = #" C:\xyz\txt";
string conv = " & 7z.exe a -tzip";
string method = " -m0=BZip2 -mx5";
This will produce:
C:\windows\system32\cmd.exe /c cd C:\Program Files(x86)\7-Zip & 7z.exe a -tzip C:\xyz\txt C:\xyz\txt -m0=BZip2 -mx5
I have sample code to run command but its not working ( just opens CMD ) without executing the command
string strCmdLine =
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
where is problem ?
You need to add a /C
Correct syntax for CMD.exe is
CMD.EXE /c command
string strCmdLine =
"/C C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
You don't need to use cmd.exe mate...
I guess this should do the job for you...
string strCmdLine =
"\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe \"";
var parmaters = "google.com";
System.Diagnostics.Process.Start(strCmdLine, parmaters);
you need the parameter "/c"
string strCmdLine =
"/c C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe " +
"--load-extension=\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\toolbar-GC\"";
System.Diagnostics.Process.Start("CMD.exe", strCmdLine);
process1.Close();
I'm trying to execute TFS via Process.Start but am having some difficulty and I can't understand why. Here's my code snippet:
/// <summary>
/// Get the entire history for a project
/// </summary>
public void GetHistory(String project)
{
ProcessStartInfo info = new ProcessStartInfo();
String fileName = Path.GetTempFileName();
info.Arguments = String.Format("history \"{0}\" /recursive /format:Detailed /noprompt > {1}", "c:\\source\\ " + project, fileName);
info.FileName = "\"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\tf.exe\"";
info.RedirectStandardError = true;
info.UseShellExecute = false;
Process process = new Process();
process.StartInfo = info;
process.Start();
process.WaitForExit();
Console.WriteLine(process.StandardError.ReadToEnd());
Console.WriteLine("History written to " + fileName);
Console.ReadKey();
}
This is resulting in a set of arguments like so (I've just removed the full project name):
I then get the following error:
The history command takes exactly one item.
If I piece the string together and execute in a normal command line however then it works:
Can anyone tell me what I'm missing?
You can't redirect output to a file in the Process.Start arguments. File redirection is a function of the shell.
If you want to put the history into a file, you will need to File.Open the file yourself, read the output of the tf history command and write it to the file.
Or you could use a command script or PowerShell script.
I am trying to use a cmd command prompt that works just fine when I use it in a normal cmd window via Windows --> Start --> cmd
My code looks like this
"C:\Program Files\Rapid-I\RapidMiner5\scripts\rapidminer.bat" -f
"C:\Users\user\.RapidMiner5\repositories\Local Repository\test.rmp"
That works just fine.
The problem is when I am trying to write this code in C# by opening a cmd window via C#
This is the c# code
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd", "/c \"C:\\Program Files\\Rapid-I\\RapidMiner5\\scripts\\rapidminer.bat\" -f \"C:\\Users\\user\\.RapidMiner5\\repositories\\Local Repository\\test.rmp\"");
You can see I quoted the same way like the code that work in the regular cmd
But I am getting a can't recognize program files.
Try this:
string fullPath = #"C:\Program Files\Rapid-I\RapidMiner5\scripts\rapidminer.bat";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = Path.GetFileName(fullPath);
p.WorkingDirectory = Path.GetDirectoryName(fullPath);
p.Arguments = #"/c -f ""C:\Users\user\.RapidMiner5\repositories\Local Repository\test.rmp""";
Process.Start(p);
I modified my original code to add arguments to your process.
One last thing you could try is putting your original command into a batch file and calling the batch file from your ProcessStartInfo object like so:
C#:
string fullPath = #"C:\Program Files\Rapid-I\RapidMiner5\scripts\run.bat";
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = Path.GetFileName(fullPath);
p.WorkingDirectory = Path.GetDirectoryName(fullPath);
Process.Start(p);
I have a jar file which I want to run from within C#.
Here's what I have so far:
clientProcess.StartInfo.FileName = #"java -jar C:\Users\Owner\Desktop\myJarFile.jar";
clientProcess.StartInfo.Arguments = "[Something]";
clientProcess.Start();
clientProcess.WaitForExit();
int exitCode = clientProcess.ExitCode;
Unfortunatly I get "System could not find specified file", which makes sense since its not a file its a command.
I've seen code online which tells you to use:
System.Diagnostics.Process.Start("java -jar myprog.jar");
However I need the return codes AND I need to wait for it to exit.
Thanks.
Finally solved it. The filename has to be java and the arguments has to contain the location of the jar file (and anything arguments you want to pass that)
System.Diagnostics.Process clientProcess = new Process();
clientProcess.StartInfo.FileName = "java";
clientProcess.StartInfo.Arguments = #"-jar "+ jarPath +" " + argumentsFortheJarFile;
clientProcess.Start();
clientProcess.WaitForExit();
int code = clientProcess.ExitCode;
You need to set environment variable Path of java.exe executable or specify the full path of java.exe.
ProcessStartInfo ps = new ProcessStartInfo(#"c:\Program Files\java\jdk1.7.0\bin\java.exe",#"-jar C:\Users\Owner\Desktop\myJarFile.jar");
Process.Start(ps);