How do I execute an ffmpeg cmd to command line using C#? - c#

I am having an issuing trying to get a form app on visual studio 19 to execute a cmd on command line for converting a video from mp4 to avi.
I am using ffmpeg for this but everytime I compile it wont pick anything up.
I have ran the argument through command line and it converts the video just fine. The path as far as I am aware is correct so I am not sure why the compiler wont pick up on any files.
private void Button1_Click(object sender, EventArgs e)
{
string cmdString = "c:\ffmpeg\bin";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "ffmpeg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = cmdString + $"-i shatner.mp4 shatner.avi";
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
}
}
The error I am getting:
"The system cannot find the specified file"
Also I would have put a try catch block around the Process.Start but it doesnt matter since it is still throwing the exception.

Your fileName and arguments are specified incorrectly. Please see below.
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "c:\\ffmpeg\\bin\\ffmpeg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-i shatner.mp4 shatner.avi";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
using (Process exeProcess = Process.Start(startInfo))
{
string error = exeProcess.StandardError.ReadToEnd();
string output = exeProcess.StandardError.ReadToEnd();
exeProcess.WaitForExit();
MessageBox.Show("ERROR:" + error);
MessageBox.Show("OUTPUT:" + error);
}
}

Related

C# - Print a PDF Error (System.ComponentModel.Win32Exception:...)

I am trying to write an application to print PDFs.
Now I get an error message where I can't get any further after a long search.
The goal would be to print a PDF file without opening the PDF-Reader.
My Code:
using System.Diagnostics;
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = #"C:\testFile.pdf";
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
The error condition occurs at p.Start(); line.
Error message:
System.ComponentModel.Win32Exception: "The system cannot find the
specified file".
New experience: 20.01.2021
If I comment out info.Verb = "print";. Then the PDF is opened.
Is this a sign that it finds the PDF but has no access to printer?
use this code it works for me :
public static void PrintToASpecificPirnter()
{
using (PrintDialog printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.OK)
{
var StartInfo = new ProcessStartInfo();
StartInfo.CreateNoWindow = true;
StartInfo.UseShellExecute = true;
StartInfo.Verb = "printTo";
StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.FileName = fileName;
Process.Start(StartInfo);
}
}
}

Execute CMD to open Remote Desktop in C#

So I am trying to use a Button to open a CMD.exe window to execute a command which allows me to automatically open a Remote Desktop with an IP from my network. I already built a piece of code, but it doesn't work. This is the code:
private void cmdRemote_Click(object sender, EventArgs e)
{
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 = "mstsc /v:" + txtIP.Text;
process.StartInfo = startInfo;
process.Start();
}
I would like to know why it doesn't execute the command.
Add "/c" in front of your arguments list, else cmd won't execute it:
cmd /c mstsc /v:...
Or, why don't you call mstsc directly?
private void cmdRemote_Click(object sender, EventArgs e)
{
var process = new System.Diagnostics.Process();
process.StartInfo = new ProcessStartInfo
{
FileName = "mstsc"
Arguments = "/v:" + txtIP.Text
}
process.Start();
}
or shorter:
Process.Start("mstsc", "/v:" + this.txtIP.Text);
Don't forget to validate the value of Text!
Instead of calling the cmd.exe, you can call the mstsc
startInfo.FileName = "mstsc"
startInfo.Arguments= " /v " + txtIP.Text

Create a process that execute powershell in C# but failed outpt

I dont know where is my missing. Even it dit not write out log. Could you fix my test code:
private void button1_Click(object sender, EventArgs e)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
startInfo.Arguments = #"/C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
//startInfo.RedirectStandardOutput = true;
//startInfo.RedirectStandardError = true;
//startInfo.UseShellExecute = false;
//startInfo.CreateNoWindow = false;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
catch (Exception ex) {
Debug.Print("Error: " + ex.Message);
}
}
Finally got it to work, code should look like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"/C powershell.exe /C Import-Module AppLocker;Set-AppLockerPolicy -XMLPolicy 'iPolicy.xml' > C:\log.txt";
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
This worked for me! You can use full paths to cmd.exe and powershell.exe if you feel necessary, it's not that important here.

Trouble running Git from C#

I'm trying to run some Git commands from a C# program using System.Diagnostics.Process and it's not working. Git is in my path and when I try to run the command at the command prompt it works fine. I've tried capturing the standard output using a nested using() statement and that's not helping. When it gets to the reader.ReadToEnd() it just shows the DOS window hung with nothing in it. Here's my code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
startInfo.WorkingDirectory = "c:\\gitmover";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = " git add \"*.*\"";
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process3.StandardOutput)
{
string result = reader.ReadToEnd();
}
}
Any ideas what I could be doing wrong?

Running a python script on C# not working

I have this code that I'm trying to run a Python script. It works correctly when I do it manually via the command prompt but if I try to do it via a button click in a C# Windows form it doesn't work.
private void btnGenerateAndrowarn_Click(object sender, EventArgs e) {
string[] filePaths = Directory.GetFiles(#"C:\Users\User1\Desktop\Android Tools\androwarn\Samples", "*.apk");
foreach (string fileName in filePaths) {
ProcessStartInfo startInfo;
Process process;
string directory = #"C:\Users\User1\Desktop\Android Tools\androwarn\";
string script = "androwarn.py";
startInfo = new ProcessStartInfo("python");
startInfo.WorkingDirectory = directory;
startInfo.Arguments = directory + script + " -i " + fileName + " -r html -v 3";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
process = new Process();
process.StartInfo = startInfo;
process.Start();
}
}
Most likely is that python is not in your %PATH%.

Categories