c# and ffpmeg command line trouble - c#

I have a php code that calls ffmpeg from the windows command line (because I run apache locally) with:
//ffmpeg -i input.mp4 -r 20 output.gif
echo exec('C:\ffmpeg\bin\ffmpeg.exe -i videos/file.avi -r 10 -s 450x230 videos/file.gif');
And it works fine! Now I wrote a C# program and I try to do the same thing with:
System.Diagnostics.Process.Start
but it fails.
Question: How do I execute the same command from the PHP code but from inside a C# code?

Process process = Process.Start(
new ProcessStartInfo
{
FileName = #"C:\ffmpeg\bin\ffmpeg.exe",
Arguments = "-i videos/file.avi -r 10 -s 450x230 videos/file.gif",
WindowStyle = ProcessWindowStyle.Hidden
}
);
process.WaitForExit();
If it fails an exception will be raised, e.g.
System.ComponentModel.Win32Exception with HResult=-2147467259 if the executable wasn't found.

Related

Aspera Error :- ascp.exe: Failed to open TCP connection for SSH, exiting

I am working on Aspera file upload functionality and I am getting one error as per below scenario.
I downloaded Aspera CLI from below URL.
https://downloads.asperasoft.com/en/downloads/62
and then with the help of command prompt ran below statement and it worked fine.
cmd Statement : ascp -P33001 -QT -l500m --file-manifest=text -k 0 -o
Overwrite=always {Source File Path} {username}#{domain}:{destination
Folder}
It worked perfectly fine.
Then I tried the same thing from console app with below code.
Process process = new Process();
process.StartInfo.FileName = #"E:\Projects\Research\AsperaFileUpload\AsperaFileUpload\AsperaLibrary\bin\ascp.exe";
process.StartInfo.Arguments = #"ascp -P33001 -QT -l500m --file-manifest=text -k 0 -o Overwrite=always {Source File Path} {username}#{domain}:{destination Folder}"; // 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);
Console.ReadLine();
process.WaitForExit();
But this code always give error like "ascp.exe: Failed to open TCP connection for SSH, exiting"
I am wondering if same command working from command line why it is not working from console app.
Can anyone please help me on this.
Thanks in advance.
This error message tells that "ascp" cannot connect to TCP port 33001 (arg "-P") of the server "{domain}"
I suggest the following:
use the more modern syntax:
ascp -P33001 -QT -l500m --file-manifest=text -k 0 -o Overwrite=always --host={domain} --user={username} --mode=send {Source File Path} {destination Folder}
add argument -L /path/to/log/folder and look at log error in file: aspera-scp-transfer.log
instead of using bare "ascp", which you will find limited, prefer to use the "TransferSDK" here:
https://developer.ibm.com/aspera

External process execution argument issue in C#

I am trying to send commands to RDP connections from a C# console application using PsExec, this is the command
PsExec.exe //1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe
wich works perfectly, runs RemoteAppExe.exe on that system, the problem is that within C# it dosen't work, here is my code :
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = #"PsExec.exe";
pProcess.StartInfo.Arguments = "//1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();
What is the problem? I assume it might be the fact that arguments are not properly escaped.
Any ideeas?
Thanks.
It should be:
pProcess.StartInfo.Arguments = #"\\1.2.3.4 -u administrator -p secredpassword -c RemoteAppExe.exe";
Changing the argument line made the code work for me.

Win32 exception in C# application

I am using the following code:
Process process = new Process();
ProcessStartInfo info = new ProcessStartInfo(#"java -jar path\Ontologizer.jar -g path\go.obo -a path\gene_association.fb -m Benjamini-Hochberg -c Parent-Child-Intersection -p path\back.txt -s path\genes.txt -o path\outfull.txt");
process.StartInfo = info;
process.Start();
process.WaitForExit();
process.Dispose();
I get a Win32 exception:
The system cannot find the file specified
How can I fix this problem?
First argument of ProcessStartInfo constructor should be file name only. All arguments to application should be putted into second argument of ProcessStartInfo constructor:
new ProcessStartInfo("java", #"-jar path\Ontologizer.jar -g path\go.obo -a path\gene_association.fb -m Benjamini-Hochberg -c Parent-Child-Intersection -p path\back.txt -s path\genes.txt -o path\outfull.txt");

Unable to run VB script on a remote server using c# Asp.Net

I am trying to Run DelTemp-Final.vbs on a remote server which is being imported from a web portal.
I tried to use native C# to get the task done, No luck. So I opted for PS tools to help me out. I am stuck with he below scenario. Please help
Psexec is working using command prompt, but not working with c#
Below is the command prompt output which is working fine:
C:\inetpub\wwwroot\DelTemp\Scripts>psexec \\testusit1 -u XXXX\xxxxxxx -p xxxx
Xxxxxxxx -accepteula -i 0 -d c:\windows\system32\cscript.exe /nologo "\\testusi
t2\C$\karthik\DelTemp-Final.vbs"
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\system32\cscript.exe started on testusit1 with process ID 5452.
C:\inetpub\wwwroot\DelTemp\Scripts>psexec \\testusit2 -u XXXX\xxxxxxx -p xxxxx
xxxxxxxxx -accepteula -i 0 -d c:\windows\system32\cscript.exe /nologo "\\testusi
t2\C$\karthik\DelTemp-Final.vbs"
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
c:\windows\system32\cscript.exe started on testusit2 with process ID 7416.
Whereas, the same command does not work when included with c# in asp.net
Process p=new Process();
p.StartInfo.UseShellExecute=false;
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardError=true;
p.StartInfo.RedirectStandardInput=true;
p.StartInfo.FileName=#"C:\inetpub\wwwroot\DelTemp\Scripts\PsExec.exe";
p.StartInfo.Arguments="\\\\"+li.Text+" -u XXXX\\xxxxxx -p xxxxxxxxxxx -accepteula -i 0 -d c:\\windows\\system32\\cscript.exe /nologo \\\\testusit2\\C$\\karthik\\DelTemp-Final.vbs";
p.Start();
string output=p.StandardOutput.ReadToEnd();
string errormessage=p.StandardError.ReadToEnd();
p.WaitForExit();
txtValueA.Text +="PSexec argument :" + p.StartInfo.Arguments;
txtValueA.Text +="<br/> Output : " + output+"| error messsage: "+errormessage;
Output of the above code for two servers is as follows:
PSexec argument :\\testusit1 -u XXXX\xxxxxxxxxx -p xxxxxxxxxx -accepteula -i 0 -d
c:\windows\system32\cscript.exe /nologo \\testusit2\C$\karthik\DelTemp-Final.vbs Output : | error
messsage: PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com The handle is invalid. Connecting to testusit1... Starting
PSEXESVC service on testusit1... Connecting with PsExec service on testusit1... Error deriving
session key:
PSexec argument :\\testusit2 -u XXXX\xxxxxxxxxxx-p xxxxxxxxxx -accepteula -i 0 -d
c:\windows\system32\cscript.exe /nologo \\testusit2\C$\karthik\DelTemp-Final.vbs Output : | error
messsage: PsExec v2.11 - Execute processes remotely Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com Access is denied. Connecting to testusit2... Starting PSEXESVC
service on testusit2... Could not start PSEXESVC service on testusit2: Connecting to testusit2...
Starting PSEXESVC service on testusit2...
Please assist.. Is there any changes I need to do to the script? Any help is greatly appreciated.
Thanks a lot in advance :)
Try directing your output to an event handler instead. I just ran a variation of this on my server with psexec executing ipconfig on the remote machine and I was able to view all output.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\inetpub\wwwroot\DelTemp\Scripts\PsExec.exe";
p.StartInfo.Arguments = "\\\\" + li.Text + " -u XXXX\\xxxxxx -p xxxxxxxxxxx -accepteula -i 0 -d c:\\windows\\system32\\cscript.exe /nologo \\\\testusit2\\C$\\karthik\\DelTemp-Final.vbs";
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
txtValueA.Text += "PSexec argument :" + p.StartInfo.Arguments;
txtValueA.Text += "<br/> Output : " + output + "| error messsage: " + errormessage;
}
public static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
//Standard output event handler for PSEXEC
public static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}

Running a Linux Console Command in C#

I am using the following code to run a Linux console command via Mono in a C# application:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c ls");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
String result = proc.StandardOutput.ReadToEnd();
This works as expected. But, if i give the command as "-c ls -l" or "-c ls /path" I still get the output with the -l and path ignored.
What syntax should I use in using multiple switches for a command?
You forgot to quote the command.
Did you try the following on the bash prompt ?
bash -c ls -l
I strongly suggest to read the man bash.
And also the getopt manual as it's what bash use to parse its parameters.
It has exactly the same behavior as bash -c ls
Why? Because you have to tell bash that ls -l is the full argument of -c, otherwise -l is treated like an argument of bash.
Either bash -c 'ls -l' or bash -c "ls -l" will do what you expect.
You have to add quotes like this:
ProcessStartInfo procStartInfo = new ProcessStartInfo("/bin/bash", "-c 'ls -l'");

Categories