I'm executing a jar file from a c# process. This is how I start the process and redirect the Input & Output. Problem indicated at the bottom!
Process process = new Process();
process.EnableRaisingEvents = false;
if (isLinux)
{
process.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory() + "/Servers/" + server.ServerLocation + "/";
}
else
{
process.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory() + "\\Servers\\" + server.ServerLocation + "\\";
}
process.StartInfo.FileName = "java";
process.StartInfo.Arguments = "-Xms" + server.AllocatedRam + "M -Xmx" + server.AllocatedRam + "M -jar " + '"' + path + '"';
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
LogController.Notice("Server Starting", server.Name);
proc = process;
process.Start();
if (Write.ContainsKey(server.ID))
{
Write.Remove(server.ID);
Write.Add(server.ID, process.StandardInput);
}
else
{
Write.Add(server.ID, process.StandardInput);
}
if (Output.ContainsKey(server.ID))
{
Output.Remove(server.ID);
Output.Add(server.ID, process.StandardOutput);
}
else
{
Output.Add(server.ID, process.StandardOutput);
}
Dictionaries
public static Dictionary<string, StreamWriter> Write = new Dictionary<string, StreamWriter>();
public static Dictionary<string, StreamReader> Output = new Dictionary<string, StreamReader>();
Executing command without closing writer
StreamWriter w = ServerController.Write[ID];
w.Write(Command.command);
w.Flush();
^ Flushing this will not execute the write. If I dispose the writer then I cannot execute more than 1 command. The writer has to always be open for writes and be able to execute the write but flushing does not work.
Related
I am running doors.exe using Process class. When I run it in the console application, there is no problem, but the program hangs in the web application. What could be the reason for this?
here is my code below;
public string ExecuteDoorsScript(string dxlScriptPath, string userName, string password)
{
//Start the process
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = #"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe";
info.Arguments = "-b \"" + dxlScriptPath + "\" -u " + userName + " -P " + password + "-W -d 1234567#databasepath.it";
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
var process = Process.Start(info);
//Wait for the window to finish loading.
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError.ReadToEnd();
//Wait for the process to end.
process.WaitForExit();
if (!String.IsNullOrEmpty(error))
{
throw new Exception("Doors run dxl error: " + error);
}
return output;
}
my dxl script;
string topfoldername = "/Test"
void listIndices(Folder parent, int level)
{
Item i
Folder f
for i in parent do {
f = folder(i)
cout << (uniqueID i) ";" (name i) ";" ((type i)) ";" "\n"
}}
listIndices(folder topfoldername, 1)
I'm currently using the following code to add tags to an image using exiftool.
This works fine.But when the image already has tags,they are not replaced.
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "exiftool.exe";
startInfo.Arguments = "-m -stay_open True -# " + tempDirectory + "\\commands.txt";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process processTemp = new Process();
processTemp.StartInfo = startInfo;
processTemp.EnableRaisingEvents = true;
processTemp.Start();
return true;
Writing commands to a text file that is monitored by exiftool
writer = new FileInfo(tempDirectory + "commands.txt").AppendText();
writer.WriteLine("-subject=" + tempstring);
writer.WriteLine(outdir + "\\" + x.Value.ElementAt(0) + "\\" + Path.GetFileName(x.Key));
writer.WriteLine("-overwrite_original");
if (counter != files.Count)
{
writer.WriteLine("-execute");
}
else
{
writer.WriteLine("-stay_open");
writer.WriteLine("False");
}
I am making a program that seeks out secured PDFs in a folder and converting them to PNG files using ImageMagick. Below is my code.
string WorkDir = #"C:\Users\rwong\Desktop\TestFiles";
Directory.SetCurrentDirectory(WorkDir);
String[] SubWorkDir = Directory.GetDirectories(WorkDir);
foreach (string subdir in SubWorkDir)
{
string[] filelist = Directory.GetFiles(subdir);
for(int f = 0; f < filelist.Length; f++)
{
if (filelist[f].ToLower().EndsWith(".pdf") || filelist[f].EndsWith(".PDF"))
{
PDFReader reader = new Pdfreader(filelist[f]);
bool PDFCheck = reader.IsOpenedWithFullPermissions;
reader.CLose();
if(PDFCheck)
{
//do nothing
}
else
{
string PNGPath = Path.ChangeExtension(filelistf], ".png");
string PDFfile = '"' + filelist[f] + '"';
string PNGfile = '"' + PNGPath + '"';
string arguments = string.Format("{0} {1}", PDFfile, PNGfile);
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe");
startInfo.Arguments = arguments;
Process.Start(startInfo);
}
}
}
I have ran the raw command in command prompt and it worked so the command isn't the issue. Sample command below
"C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe" "C:\Users\rwong\Desktop\TestFiles\Test_File File_10.PDF" "C:\Users\rwong\Desktop\TestFiles\Test_File File_10.png"
I looked around SO and there has been hints that spaces in my variable can cause an issue, but most of those threads talk about hardcoding the argument names and they only talk about 1 argument. I thought adding double quotes to each variable would solve the issue but it didn't. I also read that using ProcessStartInfo would have helped but again, no dice. I'm going to guess it is the way I formatted the 2 arguments and how I call the command, or I am using ProcessStartInto wrong. Any thoughts?
EDIT: Based on the comments below I did the extra testing testing by waiting for the command window to exit and I found the following error.
Side note: I wouldn't want to use GhostScript just yet because I feel like I am really close to an answer using ImageMagick.
Solution:
string PNGPath = Path.ChangeExtension(Loan_list[f], ".png");
string PDFfile = PNGPath.Replace("png", "pdf");
string PNGfile = PNGPath;
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\ImageMagick-6.9.2 Q16\convert.exe";
process.StartInfo.Arguments = "\"" + PDFfile + "\"" +" \"" + PNGPath +"\""; // Note the /c command (*)
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
//* Read the output (or the error)
string output = process.StandardOutput.ReadToEnd();
Console.WriteLine(output);
string err = process.StandardError.ReadToEnd();
Console.WriteLine(err);
process.WaitForExit();
It didn't like the way I was formatting the argument string.
This would help you to run you command in c# and also you can get the result of the Console in your C#.
string WorkDir = #"C:\Users\rwong\Desktop\TestFiles";
Directory.SetCurrentDirectory(WorkDir);
String[] SubWorkDir = Directory.GetDirectories(WorkDir);
foreach (string subdir in SubWorkDir)
{
string[] filelist = Directory.GetFiles(subdir);
for(int f = 0; f < filelist.Length; f++)
{
if (filelist[f].ToLower().EndsWith(".pdf") || filelist[f].EndsWith(".PDF"))
{
PDFReader reader = new Pdfreader(filelist[f]);
bool PDFCheck = reader.IsOpenedWithFullPermissions;
reader.CLose()l
if(!PDFCheck)
{
string PNGPath = Path.ChangeExtension(filelistf], ".png");
string PDFfile = '"' + filelist[f] + '"';
string PNGfile = '"' + PNGPath + '"';
string arguments = string.Format("{0} {1}", PDFfile, PNGfile);
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.EnableRaisingEvents = true;
p.StartInfo.CreateNoWindow = true;
p.startInfo.FileName = "C:\Program Files\ImageMagick-6.9.2-Q16\convert.exe";
p.startInfo.Arguments = arguments;
p.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
//You can receive the output provided by the Command prompt in Process_OutputDataReceived
p.Start();
}
}
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
string s = e.Data.ToString();
s = s.Replace("\0", string.Empty);
//Show s
Console.WriteLine(s);
}
}
i am genrating pdf from html code.Here i find code for generating using wkhtmltopdf
private void WritePDF(string HTML)
{
string inFileName,
outFileName,
tempPath;
Process p;
System.IO.StreamWriter stdin;
ProcessStartInfo psi = new ProcessStartInfo();
tempPath = Request.PhysicalApplicationPath + "ExcelFiles\\";
inFileName = Session.SessionID + ".htm";
outFileName = Session.SessionID + ".pdf";
// run the conversion utility
psi.UseShellExecute = false;
psi.FileName = "E:\\wkhtmltopdf";
psi.CreateNoWindow = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
// note that we tell wkhtmltopdf to be quiet and not run scripts
// NOTE: I couldn't figure out a way to get both stdin and stdout redirected so we have to write to a file and then clean up afterwards
psi.Arguments = "-q -n - " + tempPath + outFileName;
p = Process.Start(psi);
try
{
stdin = p.StandardInput;
stdin.AutoFlush = true;
stdin.Write(HTML);
stdin.Close();
if (p.WaitForExit(15000))
{
// NOTE: the application hangs when we use WriteFile (due to the Delete below?); this works
Response.BinaryWrite(System.IO.File.ReadAllBytes(tempPath + outFileName));
//Response.WriteFile(tempPath + outFileName);
}
}
finally
{
p.Close();
p.Dispose();
}
// delete the pdf
System.IO.File.Delete(tempPath + outFileName);
}
I found this answer from here
how to pass html as a string using wkhtmltopdf?
Here i get error Could not find file on
if (p.WaitForExit(15000))
{
// NOTE: the application hangs when we use WriteFile (due to the Delete below?); this works
Response.BinaryWrite(System.IO.File.ReadAllBytes(tempPath + outFileName));
//Response.WriteFile(tempPath + outFileName);
}
how i can create file for this now?
It gets Created on stdin.Close();
Please check if the html in stdin.Write(HTML); has some content in it
I have requirement to execute the command line arguments. If file path contains the Spaces it doesn’t work properly. It returns the error file not found. The program is given below.
public void Method()
{
string docFile = #"C:\Test Document1.doc";
string docxFile = #"C:\Test Document1.docx";
string file = #"C:\doc2x_r649 (1)\doc2x_r649\doc2x.exe";
ExecuteCommand(file, string.Format(docFile + " -o " + docxFile));
}
public static string ExecuteCommand(string file, string command)
{
String result;
try
{
//Create a new ProcessStartInfo
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo();
//Settings
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.FileName = file;
procStartInfo.Arguments = command;
//Create new Process
System.Diagnostics.Process proc = new System.Diagnostics.Process();
//Set ProcessStartInfo
proc.StartInfo = procStartInfo;
//Start Process
proc.Start();
//Wait to exit
proc.WaitForExit();
//Get Result
result = proc.StandardOutput.ReadToEnd();
//Return
return result;
}
catch
{
}
return null;
}
If file path doesn't contains spaces it works properly.
Have you tried adding quotes to your paths?
ExecuteCommand(file, string.Format("\"" + docFile + "\" -o \"" + docxFile + "\""));
Try this
ExecuteCommand(file, string.Format("\"{0}\" -o \"{1}\"",docFile , docxFile));