Seeing console output of a sub process started by a .net application - c#

My application creates a set of sub processes that runs in the background. These processes all have logging both to file and to console. Is it posssible to open up cmd and see the console output of a process after it has been created outside of cmd?
var p = new Process
{
StartInfo =
{
FileName = path,
Arguments = arguments,
WindowStyle = ProcessWindowStyle.Hidden
}
};
p.Start();

You can't access the console of the sub-process, but you can redirect the output (stdout and stderr, ideally), and pipe them to your own console/display. An example of redirecting stdout of a sub-process is shown in full on MSDN

Related

Tshark redirect output to C# console program or to file

I am trying to capture a CDP packet with Tshark commandline application and either redirect the output to my C# console program or to a file that I can then read into my application. This does not work. I am running Windows 10 version 1703.
I have verified in a commandline prompt that the specific tshark command works and that I get the correct output for the CDP packet, however when I try to redirect the output in the commandline prompt, a file gets written, but no data gets written. This issue also occurs in my C# console application and it happens whether or not I redirect the output to my console or to a file. I have read somewhere (can't remember where) that tshark might have an issue with redirecting output, do you know if this is true? I have also tried redirecting output by writing a file with the tshark -W "filename.txt" command (same result with .pcap). This fails as well.
// Nic.Name = Ethernet (the network card I am capturing packet from)
// In the code below I am trying to redirect the output from the tshark command to my console application.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = #"C:\tshark\\tshark.exe";
psi.Arguments = "-i " + nic.Name + "
ether[16:4] = 0x0300000C and ether[20:2] == 0x2000";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
Process tsharkProcess = Process.Start(psi);
string s = tsharkProcess.StandardOutput.ReadToEnd();
Console.WriteLine(s);
I expect the output to be something like this (a commandline window should not appear when running the tshark command, this should run in the background):
Capturing on 'Ethernet'
1 0.000000 d0:c7:89:1c:55:19 → 01:00:0c:cc:cc:cc CDP 492 Device ID: SW1.local.it Port ID: GigabitEthernet1/0/25
I get the following output:
Capturing on 'Ethernet'
I get no error messages.
I have found the issue. I was missing either a .dll or .exe file that tshark depends on for writing files. Writing to a file works now, but I did not get redirection to console to work.

Process.StandardOutput stops reading

I am trying to run a process, hide its window and then print its output in my program. I am achieving it with this code:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = path,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
Verb = "runas"
}
};
proc.Start();
while(!proc.StandardOutput.EndOfStream)
{
string line = proc.StandardOutput.ReadLine();
Console.WriteLine(line);
}
It works with other programs but for this one it's stopping at some point. My guess is that it sends too much messages, because before actually loading it spams a ton of messages that say: "Initializing... n%". It reads it until it reaches around 90-95% and then stops reading. My guess is that it can't read anymore because it sent too much messages. What can I do to read the whole output?
I found out how to make a workaround. It turns out the program actually needed input inbetween the initialization and the actual result. Thanks to #Alexandru Clonțea I decided that I would not run the process, but create a .bat file, which contains the following command:
program.exe > output.txt < input.txt
Where program.exe is the executable, output.txt is the file that is going to contain the output of the program and input.txt in my case is just an empty text file so I can just get some kind of an input and make the program finish.

How to execute Git commands using cmd.exe being called from another application?

I have an application that generates some files. Once the files are generated, i want to then perform some Git commands to start a local repository. I have spent a few days on multiple solutions and endless googling but i can't get this to work as expected.
If i manually kick off the .exe, locally or on a server, it works perfectly. However, when the application(MVC) calls the .exe it doesn't work locally or on a server. I will provide the code that calls the .exe itself and then what the .exe code is doing.
MVC call to run .exe
//Call git exe
var processInfo = new ProcessStartInfo()
{
Arguments = Arg1 + " " + Arg2,
UseShellExecute = false,
FileName = #"C:\Foo.exe"
};
var process = new Process()
{
StartInfo = processInfo
};
process.Start();
process.WaitForExit();
This .exe gets launched without issue so this part is working i think. I checked it via task manager.
Git command execution
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
UseShellExecute = false,
WorkingDirectory = Properties.Settings.Default.OutPutDirectory
},
};
process.Start();
using (var writer = process.StandardInput)
{
writer.WriteLine("git init");
writer.WriteLine("git add --all");
writer.WriteLine("git commit --author=\"" + ParseUserNameFromEmail() + " <" + _userEmailAddress + ">\" -m \"Initial Commit\"");
}
If i execute this part manually from the cmd prompt it works perfect on both local and server. But as soon as this .exe is called from an MVC app, it runs but the git repo isn't created as expected. I am at a total lost. I have tried running the process with my creds and a service account. I have also tried capturing the output in hope to shed light on what the issue could be but it's just empty on error and output redirects :(.

Printing a PDF from ASP .Net (c#) using Acrobat Reader

I am trying to print a PDF using ASP (C#) through Adobe Reader -the problem is it does work on my local machine but not on server. On my local it starts Adob eReader in minimized state and I can see the file present inside Printer's "See what's printing" window. But on the server I can see the process has started from Task Manager but there's no UI visible as well no file in printer's list.
I though it may be a permission issue but after trying the following steps - it still does not work.
What I have tried.
As by default it runs under DefaultAppPool user - so I created a new App pool under the admin user, it now starts the process under admin but still I can't see the UI and no output on printer.
I added permission "Allow service to interact with desktop" to IIS Admin Service following this article https://support.microsoft.com/en-us/help/555134 - but no difference.
My current code
string args = string.Format("/s /o /h /t \"{0}\" \"{1}\"", filepath, printerName);
var startInfo = new ProcessStartInfo {
FileName = Properties.Settings.Default.AdobeReaderPath,
Arguments = args,
CreateNoWindow = true,
ErrorDialog = false,
UseShellExecute = false,
Verb = "print",
WindowStyle = ProcessWindowStyle.Minimized,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
var process = Process.Start(startInfo);
In the Application Pool Advanced settings, make sure you set the option
"Load User Profile" to True on the server.

Invoke pdftohtml.exe via C#

I want to convert a pdf file into an html file, so that I can extract the values in a table.
pdftohtml.exe can do this.
If I call the following on a command prompt I get an html page with the content from the pdf file:
pdftohtml.exe test.pdf test.html
This works as expected. Now I want to invoke this exe via C#.
I did the following:
string filename = #"C:\Temp\pdftohtml.exe";
Process proc = Process.Start(filename, "test.pdf test.html");
Unfortunately this does not work. I suspect that somehow the parameters are not past to the exe correctly.
When I call this exe via the command line with -c before the parameters I get an error:
pdftohtml.exe -c test.pdf test.html
leads to an error (rangecheck in .putdeviceprops).
Does someone know how to correctly invoke this program?
You can use the following stuff,
using System.Diagnostics;
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = arguments;
// Enter the executable to run, including the complete path
start.FileName = ExeName;
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
// Retrieve the app's exit code
exitCode = proc.ExitCode;
}
Usually /C will be used to execute the command and then terminate. In the above code, do modifications as required.

Categories

Resources