Exec imagemagick convert via C# - c#

When I open a command window in windows and use the imagemagick convert command:
convert /somedir/Garden.jpg /somedir/Garden.png
It works as expected.
What I am trying to do is executing the same command as above using C#.
I tried using System.Diagnostics.Process, however, no foo.png gets created.
I am using this code:
var proc = new Process
{
StartInfo =
{
Arguments = string.Format("{0}Garden.jpg {1}Garden.png",
TEST_FILE_DIR,
TEST_FILE_DIR),
FileName = #"C:\xampp\ImageMagick-6.5.4-Q16\convert",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = false,
RedirectStandardError = false
}
};
proc.Start();
No exception gets thrown, but no .png is written, either.

My guess is that TEST_FILE_DIR contains a space - so you have to quote it.
Try this:
Arguments = string.Format("\"{0}Garden.jpg\" \"{1}Garden.png\"",
TEST_FILE_DIR,
TEST_FILE_DIR)
You might also want to give the filename including the extension, e.g.
FileName = #"C:\xampp\ImageMagick-6.5.4-Q16\convert.exe"

Related

Run exe with arguments from c#

I have referred many articles before posting this question. In my case, my exe "abc" need to pass the file name as "--run" parameter.
If I call this code from windows run window
c://path/abc.exe --run filename.json
It works but if I try to run from ProcessStart using
Process p = Process.Start(new ProcessStartInfo(#"c://path/abc.exe")
{
Arguments = "--run filename.json",
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardError = true
});
It's not working. It just opens a window for a sec and then closes it and in the background also nothing happened.
Where is your filename.json located?
Have you tried setting up ProcessStartInfo.WorkingDirectory?
Can you please try passing the arguments with double quote, i.e :
Process p = Process.Start(new ProcessStartInfo(#"C://Users/source/repos/ConsoleApp5/bin folder/ConsoleApp5.exe")
{
Arguments = "\"--run filename.json\"",
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardError = true
});
Looking at github for Process.cs : here , it is calling CreateProcessWithLogonW function which seems to use space as delimiter to pass arguments. I tried with a simple console app, and I am able to get the output as follows:
--run filename.json

Get and Set the position in Process.StandardOutput

See as an example I open CMD and navigate to the folder I want to get the data from then I use it to open the app with arugents with standard input(all synchronously) the code so far
public static Process Start(bool DoNotShowWindow = false)
{
ProcessStartInfo cmdStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = DoNotShowWindow
};
Process cmdProcess = new Process
{
StartInfo = cmdStartInfo,
EnableRaisingEvents = true
};
return cmdProcess;
}
//in other method
Process cli = InteractWithConsoleApp.Start();
cli.Start();
cli.StandardInput.WriteLine("cd /");
cli.StandardInput.WriteLine("cd " + path);
cli.StandardInput.WriteLine("fantasygold-cli getbalance abc");
Thread.Sleep(5000);
Problem
Now when I use StandardOutput.Readline, it starts from the beginning and returns me everything like first two lines of copyright,empty lines and even the input which in my case, after waiting 5 secs for the result I want to read line or to the end depending the input from where I had inputted.
possible solution
One solution I found was to change the position but it turned out it doesn't support it and even copying to another stream reader doesn't works(the position is not by line).
Well I can use filters like check a double or for an address starts with F and has a length of 36. The problem comes when I want to get the whole JSON say for like the past transactions, for which I think using filters like '{' and then check for '}'Caveat in this would be bad code, which I don't want.
TLDR
So, what could be the solution to my problem here :)
I found the answer, to open the file in subdirectory just use this cli.StartInfo.FileName = Directory.GetCurrentDirectory() + #"\SubfolderName\fantasygold-cli";
and the arguements like getbalance as cli.StartInfo.Arguments = "getbalance amunim"

How to let the command window open while executing command prompt scripts from C#?

I am facing a strange issue. I have an application in which a method executes some of command prompt commands but they are not executing in an appropriate way. The code snippet looks like
private void encryptAndWrite(String FileName)
{
string strEntry = FileName.Replace("\\web.config", ""); // Let the user assign to this string, for example like "C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb"
Process p = new Process()
{
StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
p.Start();
p.StandardInput.WriteLine(#"cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319");
p.StandardInput.WriteLine("aspnet_regiis.exe -pc \"CustomKeys12\" -exp");
p.StandardInput.WriteLine("aspnet_regiis.exe -pa \"CustomKeys12\" \"NT AUTHORITY\\NETWORK SERVICE\"");
p.StandardInput.WriteLine("aspnet_regiis -pef \"connectionStrings\" {0} -prov \"CustomEncryptProvider\"",strEntry);
p.StandardInput.WriteLine("aspnet_regiis -px \"CustomKeys12\" {0} -pri",KeyFileName);
p.StandardInput.WriteLine("exit");
Thread.Sleep(7000);
updateTextBox.ReadOnly = true;
updateTextBox.Text = String.Empty;
updateMessageLabel.Text = String.Empty;
showConfig(FileName);
}
Essentially the last command should overwrite a file which is supplied by
KeyFileName
each time the command executes successfully. But it is not.
If I hard code it like
p.StandardInput.WriteLine("aspnet_regiis -px \"CustomKeys12\" \"C:\\Users\\Chiranjib\\Desktop\\XMLKey\\CustomEncryptKey.xml\" -pri");
it executes successfully.
So is there a way where I can let the command window open so that I can see where the commands are going wrong ? I though tried CreateNoWindow = false
but it does not show the command by command execution.
Thanks in advance.

How to redirect STDIN .NET Process before Starting the process

Im trying to make C# application that uses hunpos tagger.
Runing hunpos-tag.exe requires three input arguments: model, inputFile, outputFile
In cmd it would look something like this:
hunpos-tag.exe model <inputFile >outputFile
If I just run it with model it writes something and waits for end command. When i tried using standard redirect I either get an exception (i solved this the code was off by a braceket i just get the or scenario now) or I get the results of running the tagger with just model argument. Here's the code:
string inputFilePath = path + "\\CopyFolder\\rr";
string pathToExe = path + "\\CopyFolder\\hunpos-tag.exe";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
RedirectStandardInput = true,
WorkingDirectory = Directory.GetDirectoryRoot(pathToExe),
Arguments = path + "\\CopyFolder\\model.hunpos.mte5.defnpout",
};
try
{
Process _proc = new Process();
_proc.StartInfo.FileName = pathToExe;
_proc.StartInfo.UseShellExecute = false;
_proc.StartInfo.RedirectStandardInput = true;
_proc.StartInfo.Arguments = path + "\\CopyFolder\\model.hunpos.mte5.defnpout";
//Magic goes here
_proc.Start();
_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Any ideas how can I redirect input before starting my process?
It is not only required to set RedirectStandardInput to true but also you need to use the input stream to write the text you want:
_proc.StandardInput.WriteLine("The text you want to write");
There's no need for that ProcessStartInfo if you're setting the info later on. Just get rid of that. And it seems you are already doing what you want. Just creating the process object doesn't start the process, Process.Start does. Just make a new StreamWriter and pass it Process.StandardInput (I think that's right, it may be something else)

Command Line Process

So after scouring the web I found a few articles (some on stackoverflow) which described how to execute a command line prompt by starting a new process in c#. The second argument, which I've commented out, works just fine, but the one I actually need (the first one) doesn't. It returns the error "Could not find or load main class edu.stanford.nlp.parser.lexparser.LexicalizedParser" When I open up a command line (non-programatically) and then execute the same command (aside from the escaped quotations) it works great. Any idea's about what the problem could be? Thanks!
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "CMD.exe",
Arguments = "/c java -mx100m -cp \"*\" edu.stanford.nlp.parser.lexparser.LexicalizedParser edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz libtest.txt",
// Arguments = "/c echo Foo",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
Console.WriteLine(proc.StandardOutput.ReadToEnd());
Console.WriteLine(proc.StandardError.ReadToEnd());
Ensure that the executing path where you start your process is correct!
You can use Process Monitor from SysInternals to figure out where that class is looked for.

Categories

Resources