I am open WireShark using command line and start capture the packets, when i do it using CMD windows i can see the number of the incoming packets and this number i want to show in my application form (win form), currently this is my code but my application crash with error
static void Main(string[] args)
{
try
{
string _pcapPath = #"C:\test.pcap";
Process _tsharkProcess = new Process();
_tsharkProcess.StartInfo.FileName = #"C:\Program Files\Wireshark\tshark.exe";
_tsharkProcess.StartInfo.Arguments = string.Format(" -i " + 2 + " -c " + int.MaxValue + " -w " + _pcapPath);
_tsharkProcess.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
_tsharkProcess.StartInfo.RedirectStandardOutput = true;
_tsharkProcess.StartInfo.UseShellExecute = false;
//_tsharkProcess.StartInfo.CreateNoWindow = true;
//_tsharkProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_tsharkProcess.Start();
StreamReader myStreamReader = _tsharkProcess.StandardOutput;
string myString = myStreamReader.ReadLine(); //read the standard output of the spawned process.
Console.WriteLine(myString);
_tsharkProcess.WaitForExit();
}
catch (Exception)
{
}
}
private static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
string srt = e.Data; //arg.Data contains the output data from the process...
}
You can try with this code
Nota : Set these lines before start
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
Code:
Process process = new Process();
process.StartInfo.FileName = #"C:\Program Files\Wireshark\tshark.exe";
process.StartInfo.Arguments = string.Format(" -i " + _interfaceNumber + " -c " + int.MaxValue + " -w " + _pcapPath);
process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
StreamReader myStreamReader = process.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
process.WaitForExit();
process.Close();
}
Related
I am using ffmpeg commands with C# processes. I used a command to change the audio of a video and it was working but I also wanted to write the output to a text file. The new command works from cmd but apparently, it does not work while using a C# process.
The command that does not work with the Process is:
ffmpeg -i videoPath -i audioPath -c:v copy -map 0:v:0 -map 1:a:0 -shortest newVideoPath > textFilePath 2>&1
And the working command is the same without the last part:
ffmpeg -i videoPath -i audioPath -c:v copy -map 0:v:0 -map 1:a:0 -shortest newVideoPath
Here is the C# code:
Process proc = new Process();
proc.StartInfo.FileName = ffmpegPath;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = "-i " + videoPath + " -i " + newAudioPath + " -c:v copy" + " -map" + " 0:v:0 " + "-map 1:a:0 " + "-shortest " + newVideoPath + " > " + #"F:\Videos\log.txt"+ " 2>&1";
proc.EnableRaisingEvents = true;
proc.Exited += UpdateVideoSource;
proc.Start();
I checked the arguments over and over again, and looked for missing spaces but nothing worked.
What can be the problem?
ffmpeg has the option -report :
Dump full command line and log output to a file named program-YYYYMMDD-HHMMSS.log in the current directory.
By default, the file generated follow the parttern program-YYYYMMDD-HHMMSS.log. To specify a file, you can set the environnement variable variable FFREPORT :
FFREPORT=file=ffreport.log:level=verbose
In C#, you can start a process with a specific environnement variables by ProcessStartInfo.EnvironmentVariables :
Process proc = new Process();
proc.StartInfo.FileName = ffmpegPath;
proc.StartInfo.RedirectStandardError = false; // Don't redirect the output
proc.StartInfo.RedirectStandardOutput = false;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
//Add the argument -report
proc.StartInfo.Arguments = "-i " + videoPath + " -i " + newAudioPath + " -c:v copy" + " -map" + " 0:v:0 " + "-map 1:a:0 " + "-shortest " + newVideoPath + " -report";
//Set the output file and the log level
proc.StartInfo.EnvironmentVariables["FFREPORT"] = "file=ffreport.log:level=verbose";
proc.EnableRaisingEvents = true;
proc.Exited += UpdateVideoSource;
proc.Start();
I haven't tested, but this is good start.
The function > is specific to cmd.
To redirect the process output to a file, you can redirect the output to the calling program that will write to a file :
void Redirect(StreamReader output, StreamWriter to)
{
string textLine;
while ((textLine = output.ReadLine()) == null)
{
to.WriteLine(textLine);
}
}
using (Process proc = new Process())
{
proc.StartInfo.FileName = ffmpegPath;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = "-i " + videoPath + " -i " + newAudioPath + " -c:v copy" + " -map" + " 0:v:0 " + "-map 1:a:0 " + "-shortest";
proc.EnableRaisingEvents = true;
proc.Exited += UpdateVideoSource;
proc.Start();
using (StreamWriter writer = File.CreateText(#"F:\Videos\log.txt"))
{
//Redirect standard and error ouput
var standardOutputThread = new Thread(new ThreadStart(() => Redirect(proc.StandardOutput, writer)));
var errorOutputThread = new Thread(new ThreadStart(() => Redirect(proc.StandardError, writer)));
//Start redirect thread
standardOutputThread.Start();
errorOutputThread.Start();
//Wait the end of redirection
standardOutputThread.Join();
errorOutputThread.Join();
}
proc.WaitForExit();
}
I'm running an app that uses a console app in the background
When I do this in cmd read-info.exe Myfile.file >fileinfo.txt it will create the file fileinfo.txt in the route folder.
But when I do it in code nothing happens, why?
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog theDialog = new FolderBrowserDialog();
theDialog.RootFolder = System.Environment.SpecialFolder.MyComputer;
if (theDialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = theDialog.SelectedPath.ToString();
string command = "read-info.exe " + textBox1.Text +"> File.txt";
string retur = CMD(command);
}
}
static string CMD(string args)
{
string cmdbat = "cd " + Application.StartupPath.Replace("\\", "/") + "\r\n";
cmdbat += args + " >> out.txt\r\n";
cmdbat += "exit\r\n";
File.WriteAllText("cmd.bat", cmdbat);
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.Arguments = "";
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Application.StartupPath;
startInfo.CreateNoWindow = true;
startInfo.FileName = Application.StartupPath + "\\cmd.bat";
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
System.Threading.Thread.Sleep(5000);
string cmdOut = File.ReadAllText("out.txt");
File.Delete("cmd.bat");
File.Delete("out.txt");
return cmdOut;
}
See this - I tnink it is what necessary: http://www.dotnetperls.com/redirectstandardoutput
Copied from the link above:
using System;
using System.Diagnostics;
using System.IO;
class Program
{
static void Main()
{
//
// Setup the process with the ProcessStartInfo class.
//
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\7za.exe"; // Specify exe name.
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
//
// Start the process.
//
using (Process process = Process.Start(start))
{
//
// Read in all the text from the process with the StreamReader.
//
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
}
use cmd /C for running console commands.
string command = "cmd /C read-info.exe " + textBox1.Text +"> File.txt";
Got It Working Thanks to #crashmstr
it outputs file as out.txt so just had to comment out File.Delete("out.txt");
I am trying to read the CGMiner output in a C# program I am writing. I successfully read/write the standard thread input/output. But for some reason CGMiner does not write to the standard cmd window output, and I can't read it in C#. Any ideas?
This is my process start:
public void start() {
proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.OutputDataReceived += (s, e) => updateConsoleOutput(e);
proc.Start();
proc.BeginOutputReadLine();
}
This is the function I use to write to the console:
public void RunCommand(string cmd = "") {
if (cmd.Length > 0) {
ConsoleInput = cmd;
}
StreamWriter myStreamWriter = proc.StandardInput;
myStreamWriter.WriteLine(ConsoleInput);
myStreamWriter.Flush();
ConsoleInput = String.Empty;
}
These are the functions I use to read from the console:
public delegate void consoleOutputCallback(string message);
private void updateConsoleOutput(DataReceivedEventArgs outLine) {
if (!String.IsNullOrEmpty(outLine.Data)) {
this.Dispatcher.Invoke(
new consoleOutputCallback(updateConsoleText),
new object[] { outLine.Data }
);
}
}
public void updateConsoleText(string message) {
this.OutputBlock.Text += message + "\n";
}
HINT: Don't know if it helps, but CGMiner will overwrite the entire console window, and cursor always stay at top left and does not move. All command before running CGMiner is overwritten.
Forgot to add, this is console command I use:
cd C:\cgminer\
del *.bin
cgminer.exe -o stratum+tcp://global.wemineltc.com:3335 -O yongke.1:x -g 2
You need to set the --per-device-stats flag in order for GPU stats to be written into stream
And don't forget to add this to the code in question
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardError = true;
proc.ErrorDataReceived += (s, e) => updateConsoleOutput(e);
....
proc.Start();
proc.BeginErrorReadLine();
Most of miners use standart Error stream instead of standart Output stream (to write both output data and errors) for some reason..
the only thing that made it work for me was
-T
here is my working code
Task StartGPUMiner(object set)
{
MinerParams m = new MinerParams();
m = (MinerParams)set;
var tcs = new TaskCompletionSource<object>();
Process p = new Process();
ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = m.ApplicationPath + "\\cgminer\\cgminer.exe";
start.Arguments = " -I " + m.GpuIntisity + " -T --scrypt -o " + m.sProtocol + m.ServerName + ":" + m.ServerPort + " -u " + m.UserName + "." + m.WorkerName + " -p " + m.ThePassword + " " + m.GpuParams;
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
start.RedirectStandardOutput = true;
start.UseShellExecute = false;
start.CreateNoWindow = true;
var proc = Process.Start(start);
proc.OutputDataReceived += (s, e) =>
{
try
{
this.Invoke((Action)delegate
{
txtLog.Text += (e.Data + Environment.NewLine);
});
}
catch { }
};
try
{
proc.Exited += (s, e) => tcs.SetResult(null);
proc.EnableRaisingEvents = true;
proc.BeginOutputReadLine();
}
catch { }
return tcs.Task;
}
I have a list of video files and I want to convert them with ffmpeg. This is my code:
public static void ConvertToMp3(String inputPath, String title)
{
String outputpath = "\"D:\\Mp3\\" + title + ".mp3\"";
String _out;
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "ffmpeg";
p.StartInfo.Arguments = " -i \"" + inputPath + "\" -vn -f mp3 -ab 192k " + outputpath;
p.Start();
p.StandardOutput.ReadToEnd();
_out = p.StandardError.ReadToEnd();
p.WaitForExit();
if(!p.HasExited)
p.Kill();
Console.WriteLine(_out);
}
It works fine, but when I call this function in a loop for n times, it opens too many processes. I want to open only one process at a time and when it's done, go to the next.
What about checking the process count and only executing your code if its less than x ( 2 in example)
int process = 0;
foreach (System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses())
{
if (myProc.ProcessName == "process name")
process++;
if (process < 2)
p.Start();
}
Before WaitForExit, add this command
p.Exited += (sender, e) =>
{
// Thread.Sleep(1000 * 60);
// Thread thread = new Thread(() => callProcess());
// thread.Start();
};
This will work when process finished. I generally use new thread.
i am build application who run Wireshark and start sniffing, Wireshark has dumpcap.exe file who receive arguments (interface number, output file etc) and start sniffing and meanwhile i can see in the cmd window the number of packet and this number growing all the time.
my question is how can i catch this number every few seconds in order to show this number on my application windows.
this is my class who start this sniffing:
public class DumpPcap
{
public int _interfaceNumber;
public string _pcapPath;
public string _dumPcapPath = #"C:\Program Files\Wireshark\dumpcap.exe";
public DumpPcap(int interfaceNumber, string pcapPath)
{
_interfaceNumber = interfaceNumber;
_pcapPath = pcapPath;
}
public void startTheCapture()
{
List<string> stList = new List<string>();
ProcessStartInfo process = new ProcessStartInfo(_dumPcapPath);
process.Arguments = string.Format("-i " + _interfaceNumber + " -s 65535 -w " + _pcapPath);
process.WindowStyle = ProcessWindowStyle.Hidden;
process.RedirectStandardOutput = true;
process.RedirectStandardError = true;
process.CreateNoWindow = true;
process.UseShellExecute = false;
process.ErrorDialog = false;
Process dumpcap = Process.Start(process);
StreamReader reader = dumpcap.StandardOutput;
//dumpcap.WaitForExit(100000);
while (!reader.EndOfStream)
{
stList.Add(reader.ReadLine());
}
}
}
and this is screenshot and i marked in red the field that i want to show in my application:
http://image.torrent-invites.com/images/641Untitled.jpg
Instead of trying to capture output text from the ProcessStartInfo how about doing it from the Process, and intercept the output data via the OutputDataReceived event handler?
Try this replacement to your block of code:
List<string> stList = new List<string>();
var process = new Process();
process.StartInfo.FileName = _dumPcapPath;
process.StartInfo.Arguments =
string.Format("-i " + _interfaceNumber + " -s 65535 -w " + _pcapPath);
process.Startinfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Startinfo.RedirectStandardOutput = true;
process.Startinfo.RedirectStandardError = true;
process.Startinfo.CreateNoWindow = true;
process.Startinfo.UseShellExecute = false;
process.Startinfo.ErrorDialog = false;
// capture the data received event here...
process.OutputDataReceived +=
new DataReceivedEventHandler(process_OutputDataReceived);
process.Start();
process.BeginOutputReadLine();
private void process_OutputDataReceived(object sender, DataReceivedEventArgs arg)
{
// arg.Data contains the output data from the process...
}
NOTE: I just typed this in without compiling or any serious validating, so be warned, LOL...