How to use wevtutil.exe command lines in a Process? - c#

This is two functions im using the wevtutil in both functions this arguments worked in a bat file but not working here i cant find any of the text files created in the contentDirectory.
Something is wrong with the Arguments i guess.
private void SystemEvents()
{
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WorkingDirectory = contentDirectory;
proc.StartInfo.Arguments = "wevtutil qe system /rd:true /f:text> eventsys.txt";
proc.Start();
proc.WaitForExit();
proc.Close();
}
private void AppEvents()
{
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WorkingDirectory = contentDirectory;
proc.StartInfo.Arguments = "wevtutil qe application /rd:true /f:text> eventapp.txt";
proc.Start();
proc.WaitForExit();
proc.Close();
}
What is wrong with the arguments ?

your cmd.exe is missing a /c agument. type cmd.exe /? in a termainal for more info.
you could just change the Arg line too:
proc.StartInfo.Arguments = "/c wevtutil qe application /rd:true /f:text";
but really I guess you want to do something like this instead.
Process proc = new Process();
proc.EnableRaisingEvents = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.FileName = "wevtutil";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.Arguments = "qe application /rd:true /f:text";
Then redirect stdout/stderr to allow you to process the output in C#.

Related

tasklist command won't exit when RedirectStandardOutput is true

I'm trying to execute a command and wait for it to exit:
Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c tasklist";
proc.Start();
proc.WaitForExit();
return;
However, tasklist command never exit if RedirectStandardOutput is set to true.
If I set RedirectStandardOutput to false or change the command to /c whoami, it exit almost immediately.
Any ideas?
You should read the output before calling WaitForExit(), this sample works fine:
Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c tasklist";
proc.Start();
var output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
Console.WriteLine(output);
return;
Without reading an output tasklist process waits until parent process read the output, according to remarks section of RedirectStandardOutput
A deadlock condition can result if the parent process calls
p.WaitForExit before p.StandardOutput.ReadToEnd and the child process
writes enough text to fill the redirected stream. The parent process
would wait indefinitely for the child process to exit. The child
process would wait indefinitely for the parent to read from the full
StandardOutput stream.
You can also do an asynchronous reading of process output (and limit a process wait for exit timeout), like this
Process proc = new Process();
StringBuilder output = new StringBuilder();
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c tasklist";
proc.OutputDataReceived += (s, e) =>
{
output.Append(e.Data).Append("\n");
};
proc.ErrorDataReceived += (s, e) =>
{
output.Append(e.Data).Append("\n");
};
proc.Start();
proc.BeginOutputReadLine();
proc.WaitForExit(100);
Console.WriteLine(output);
return;
Also, if you are going to redirect both streams (error and output), you have read at least one of them asynchronously, because of deadlocks again (MSDN link shows an example)

Error while executing command via cmd in C#

I'm using C# and I need to execute command in cmd without displaying shell window and retrieve the result, so I did some work like the following:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = command;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string output = string.Empty;
while (!proc.StandardOutput.EndOfStream) {
output = proc.StandardOutput.ReadLine();
}
But when I executed this code, console returned me this error: 'System.InvalidOperationException'(System.dll).
Why is this problem happening and how can I solve this problem?

Run CMD command without displaying it?

I have created a Process to run command in CMD.
var process = Process.Start("CMD.exe", "/c apktool d app.apk");
process.WaitForExit();
How can I run this command without displaying actual CMD window?
You can use the WindowsStyle-Property to indicate whether the process is started in a window that is maximized, minimized, normal (neither maximized nor minimized), or not visible
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
Source:
Property:MSDN
Enumartion: MSDN
And change your code to this, becaeuse you started the process when initializing the object, so the properties (who got set after starting the process) won't be recognized.
Process proc = new Process();
proc.StartInfo.FileName = "CMD.exe";
proc.StartInfo.Arguments = "/c apktool d app.apk";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
There are several issues with your program, as pointed out in the various comments and answers. I tried to address all of them here.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "apktool";
//join the arguments with a space, this allows you to set "app.apk" to a variable
psi.Arguments = String.Join(" ", "d", "app.apk");
//leave it to the application, not the OS to launch the file
psi.UseShellExecute = false;
//choose to not create a window
psi.CreateNoWindow = true;
//set the window's style to 'hidden'
psi.WindowStyle = ProcessWindowStyle.Hidden;
var proc = new Process();
proc.StartInfo = psi;
proc.Start();
proc.WaitForExit();
The main issues:
using cmd /c when not necessary
starting the app without setting the properties for hiding it
Try this :
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.WaitForExit();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "dcm2jpg.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

Run command line code programmatically using C#

I'm using this code run in windows command prompt..
But I need this done programmatically using C# code
C:\Windows\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis.exe -pdf "connection
Strings" "C:\Users\XXX\Desktop\connection string\DNN"
try this
ExecuteCommand("Your command here");
call it using process
public void ExecuteCommand(string Command)
{
ProcessStartInfo ProcessInfo;
Process Process;
ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
ProcessInfo.CreateNoWindow = true;
ProcessInfo.UseShellExecute = true;
Process = Process.Start(ProcessInfo);
}
You may use the Process.Start method:
Process.Start(
#"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe",
#"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN"""
);
or if you want more control over the shell and be able to capture for example the standard output and error you could use the overload taking a ProcessStartInfo:
var psi = new ProcessStartInfo(#"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe")
{
Arguments = #"-pdf ""connection Strings"" ""C:\Users\XXX\Desktop\connection string\DNN""",
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(psi);
You should be able to do that using a process
var proc = new Process();
proc.StartInfo.FileName = #"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe ";
proc.StartInfo.Arguments = string.Format(#"{0} ""{1}""" ""{2}""","-pdf","connection Strings" ,"C:\Users\XXX\Desktop\connection string\DNN");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string outPut = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();

ffmpeg c#asp video conversion

I am trying to convert one flv video to mp4 video. Why this
following code results error? it gives exception as "No process is
associated with this object." The parameters
"Path_FFMPEG"="E:\\Arun Kumar\\Main Project\\Advertisement Demo\\Advertisementdemo\\Advertisementdemo\\ffmpeg\\bin\\ffmpeg.exe" and "strParam"="-i E:\\Arun Kumar\\Main Project\\Advertisement Demo\\Advertisementdemo\\Advertisementdemo\\Videos\\cars1.flv -same_quant E:\\Arun Kumar\\Main Project\\Advertisement Demo\\Advertisementdemo\\Advertisementdemo\\Videos\\ConvertedFiles\\cars1.mp4"
Process ffmpeg = new Process();
ProcessStartInfo ffmpeg_StartInfo = new ProcessStartInfo(Path_FFMPEG, strParam);
ffmpeg_StartInfo.UseShellExecute = false;
ffmpeg_StartInfo.RedirectStandardError = true;
ffmpeg_StartInfo.RedirectStandardOutput = true;
ffmpeg.StartInfo = ffmpeg_StartInfo;
ffmpeg_StartInfo.CreateNoWindow = true;
ffmpeg.EnableRaisingEvents = true;
ffmpeg.Start();
ffmpeg.WaitForExit();
ffmpeg.Close();
ffmpeg.Dispose();
ffmpeg = null;
try this code (assuming your mp4 is named video):
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = //PHYSICAL path to ffmpeg (use \\ instead of \);
proc.StartInfo.Arguments = "-i video.flv video.mp4
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardOutput = false;
proc.Start();
proc.WaitForExit();
proc.Close();

Categories

Resources