C# run batch file with argument with spaces - c#

I need to run batch file, which has path (can contains spaces) as argument.
Batch file is really simple:
echo off
echo %1 > echotest.txt
Csharp code I am using to run this batch file:
ProcessStartInfo info = new ProcessStartInfo();
info.UserName = KIM_USER;
info.Password = ConvertToSecureString(KIM_USER_PASSWORD);
info.FileName = theTask.Path;
info.Arguments = "\"" + TranslateParameter(theTask.Parameter) + "\"";
info.Domain = Environment.MachineName;
info.WorkingDirectory = Path.GetDirectoryName(theTask.Path);
info.UseShellExecute = false;
info.CreateNoWindow = true;
Process batProcess = Process.Start(info);
batProcess.WaitForExit();
Basically in parameter comes e.g {Test_Path} and this is in TranslateParameter converted to real path, e.g: D:\Test Path\ (contains spaces)
This does not work for me, it returns me exit code 1 everytime.
If i remove \" from info.Arguments, it works, but in output file is just D:\Test
Any suggestions?
Regards

Can you try this if that works:
ProcessStartInfo info = new ProcessStartInfo();
info.UserName = KIM_USER;
info.Password = ConvertToSecureString(KIM_USER_PASSWORD);
info.FileName = theTask.Path + " \"" + TranslateParameter(theTask.Parameter) + "\"";
//info.Arguments = "\"" + TranslateParameter(theTask.Parameter) + "\"";
info.Domain = Environment.MachineName;
info.WorkingDirectory = Path.GetDirectoryName(theTask.Path);
info.UseShellExecute = false;
info.CreateNoWindow = true;
Process batProcess = Process.Start(info);
batProcess.WaitForExit();

Related

ffmpeg ask for intsall

I am trying to create an image/thumbnail from the video stored in local folder.
Here is what I am doing is-
Process p;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Server.MapPath("~/FFMPEG/ffmpeg.exe");
info.RedirectStandardOutput = false;
info.CreateNoWindow = false;
info.Arguments = " -i " + videopath + " -vframes 1 " + imagepath + "%d.jpg";
info.UseShellExecute = false;
p = Process.Start(info);
while (!p.HasExited) { Thread.Sleep(10); }
When I execute above code a popup box comes to install ffmpeg.exe
If I install the software, for the next time it asks again.
Am I doing some mistake?
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Below is the code
Process p;
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Server.MapPath("~/FFMPEG/ffmpeg.exe");
info.RedirectStandardOutput = false;
// below lines will not open the command window
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.Arguments = " -i " + videopath + " -vframes 1 " + imagepath + "%d.jpg";
info.UseShellExecute = false;
p = Process.Start(info);
while (!p.HasExited) { Thread.Sleep(10); }

Cannot access the file because it is being used by another process using ffmpeg

I am getting the error:
Cannot access the file because it is being used by another process
I have a C# desktop app.
I am using the Process class to convert images to a video file by using FFMPEG.
This is my code:
using (Process serverBuild = new Process())
{
serverBuild.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
string args = " -f image2 -i " + {path} + "\\img%05d.jpg -s 352x288 -filter:v \"setpts=5.0*PTS\" -y " + {path}\\File.mp4;
serverBuild.StartInfo.Arguments = args;
serverBuild.StartInfo.FileName = "ffmpeg.exe";
serverBuild.StartInfo.UseShellExecute = false;
serverBuild.StartInfo.RedirectStandardOutput = true;
serverBuild.StartInfo.RedirectStandardError = true;
serverBuild.StartInfo.CreateNoWindow = true;
serverBuild.Start();
// string output = serverBuild.StandardOutput.ReadToEnd();
//Log.Instance.Debug(serverBuild.StandardError.ReadToEnd());
serverBuild.WaitForExit();
serverBuild.Close();
}
Directory.Delete(ExportRoute + FFMPEGPacket.LicenseKey + "\\" + FFMPEGPacket.Guid, true);
//which raise the error..
The images are all deleted but the File.Mp4 is not and that is the error. The error says that the newly created MP4 file cannot be deleted.
NB
This is partial code to illustrate the error
You may try the following code to create the file (it worked for me):
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = exe_path;
// replace your arguments here
psi.Arguments = string.Format(#" arguments ")
psi.CreateNoWindow = true;
psi.ErrorDialog = true;
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = false;
psi.RedirectStandardError = true;
Process exeProcess = Process.Start(psi);
exeProcess.PriorityClass = ProcessPriorityClass.High;
string outString = string.Empty;
exeProcess.OutputDataReceived += (s, e) =>
{
outString += e.Data;
};
exeProcess.BeginOutputReadLine();
string errString = exeProcess.StandardError.ReadToEnd();
Trace.WriteLine(outString);
Trace.TraceError(errString);
exeProcess.WaitForExit();
exeProcess.Close();
exeProcess.Dispose();
FFMPEG might still be rendering the creation of video from images after it closes, so it might be worth if you place a Threading.Thead.Sleep(5000) 5 secs; before delete.
Try that:
File.WriteAllBytes(path, new byte[0]);
File.Delete(path);

process.StandardOutput.ReadToEnd returns null

I have this code:
in c# windows form works right...but in asp.net its return null
var tempCadena = HttpContext.Current.Server.MapPath("~/" + HttpContext.Current.Session["db"] + "/" + "cadena");
System.IO.File.WriteAllText(tempCadena, cadena);
// Digestion SHA1
var tempSha = HttpContext.Current.Server.MapPath("~/" + HttpContext.Current.Session["db"] + "/" + "sha");
var opensslPath = HttpContext.Current.Server.MapPath("~/openssl/bin/openssl.exe");
Process process = new Process();
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.StartInfo.FileName = opensslPath;
process.StartInfo.Arguments = "dgst -sha1 " + tempCadena;
process.StartInfo.UseShellExecute = false;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string codificado = "";
codificado = process.StandardOutput.ReadToEnd();
process.WaitForExit();
codificado = null I tried many things and nothing...can someone help me please?
I tried to change Process to ProcessStartInfo and doesn't work
I found it:
the: tempCadena has the value to the directory:
D:\ShamTec MySQL\
and it has a space in the folder name
that's why the process returns error
thanks

How to start kdiff from Console application?

UPDATED...
I want to call kdiff from Console application. So I'm building two files and want to compare they at the end of executing my program:
string diffCmd = string.Format("{0} {1}", Logging.FileNames[0], Logging.FileNames[1]);
// diffCmd = D:\vdenisenko\DbHelper\DbHelper\bin\Debug\Reports\16_Nov 06_30_46_DiscussionThreads_ORIGIN.txt D:\vdenisenko\DbHelper\DbHelper\bin\Debug\Reports\16_Nov 06_30_46_DiscussionThreads_ORIGIN.txt
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\KDiff3\kdiff3.exe", diffCmd);
//specification is here http://kdiff3.sourceforge.net/doc/documentation.html
It runs kdiff3 tool, but something wrong with filenames or command... Could you please look on screenshot and say what is wrong?
You need to use Process.Start():
string kdiffPath = #"c:\Program Files\Kdiff3.exe"; // here is full path to kdiff utility
string fileName = #"d:\file1.txt";
string fileName2 = #"d:\file2.txt";
Process.Start(kdiffPath,String.Format("\"{0}\" \"{1}\"",fileName,fileName2));
Arguments as described in the docs: kdiff3 file1 file2
var args = String.Format("{0} {1}", fileName, fileName2);
Process.Start(kdiffPath, args);
string kdiffPath = #"c:\Program Files\Kdiff3.exe"; // here is full path to kdiff utility
string fileName = #"d:\file1.txt";
string fileName2 = #"d:\file2.txt";
ProcessStartInfo psi = new ProcessStartInfo(kdiffPath);
psi.RedirectStandardOutput = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
psi.Arguments = fileName + " " + fileName2;
Process app = Process.Start(psi);
StreamReader reader = app.StandardOutput;
//get reponse from console app in your app
do
{
string line = reader.ReadLine();
}
while(!reader.EndOfStream);
app.WaitForExit();
This will run the program from your console app
Process p = new Process();
p.StartInfo.FileName = kdiffPath;
p.StartInfo.Arguments = "\"" + fileName + "\" \"" + fileName2 + "\"";
p.Start();
Unless you are trying to do something else, in which case you need to provide more details.

Multiple arguments with cmd.exe via a C# Process

I try to call the esriRegAsm.exe with arguments from a C# program.
The purpose is to register a Dll. Therefore I usually call the esriRegAsm.exe with the Dll as argument plus some additional parameters (/p:Desktop /s). This works fine if I type it into cmd.exe. Somehow I think that the process sends only the first string to the cmd and not the whole argument list, but I need the "" for the space character in the paths.
For debugging I added a message box and the strings seems to be okay.
Backslash or double backslash seems to be unimportant.
string targetDir = this.Context.Parameters["targ"];
string programFilesFolder = this.Context.Parameters["proFiles"];
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C \"" + programFilesFolder + "Common Files\\\\ArcGIS\\\\bin\\\\esriRegAsm.exe\" " + "\"" + targetDir + "RArcGISTest.dll\" /p:Desktop /s";
MessageBox.Show("/C \"" + programFilesFolder + "Common Files\\\\ArcGIS\\\\bin\\\\esriRegAsm.exe\" " + "\"" + targetDir + "RArcGISTest.dll\" /p:Desktop /s");
process.StartInfo = startInfo;
process.Start();
As I cannot attache a picture of the message box... the output is:
/C "C:\Program Files (x86)\Common Files\ArcGIS\bin\esriRegAsm.exe" "C:\install\RArcGISTest.dll" /p:Desktop /s"
Why are you double-escaping things, and why are you routing it through cmd.exe? Just execute the process directly:
string targetDir = this.Context.Parameters["targ"];
string programFilesFolder = this.Context.Parameters["proFiles"];
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = Path.Combine(programFilesFolder, #"Common Files\ArcGIS\bin\esriRegAsm.exe");
startInfo.Arguments = "\"" + Path.Combine(targetDir, "RArcGISTest.dll") + "\" /p:Desktop /s";
process.StartInfo = startInfo;
process.Start();

Categories

Resources