How to launch a single process the pipes one output into another? - c#

As the question states, I am trying to launch two processes from a single process call using c# process library.
So for example, I'd like to run something like:
application1 "arg1" "arg2" | application2 "arg1".
At the moment, I have it setup like this:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = SEEBELOW;
startInfo.FileName = FIRSTAPPLICATIONLOCATION;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = startInfo;
proc.Start();
Where the process filename is "application1" and the arguments of "application1" are " "arg1" "arg2" | "application2" "arg1".

Related

How to run an exe file using C# in console application

I am trying to run an exe file in my console application which is located on a network drive.
So what needs to happen is the app needs to map the network drive with a drive letter by using this code:
private static void MapDrive()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "net.exe";
startInfo.Arguments = #"use w: \\<server>\CompanyData\W10 /user:Administrator Password";
process.StartInfo = startInfo;
process.Start();
}
This works great and the drive letter is mapped.
Now the problem I am facing is to run the exe file with in this mapped drive.
I have tried the below but does not seem to work:
private static void RunSetup()
{
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 = #"w:\setup.exe";
process.StartInfo = startInfo;
process.Start();;
}
Nothing seems to happen in regards to launching the exe file.
I need to know what I am doing wrong here?
Thanks
Either include the "/c" argument
startInfo.FileName = "cmd.exe";
startInfo.Arguments = #"/c w:\setup.exe";
or set FileName directly to the setup.exe
startInfo.FileName = "w:\setup.exe";
as mentioned in the comments

Run .exe from another app and enter values automatically

I want to run .exe file from another app, which is console app in .NET Core. When the app is open I want to write input to console from my code. Something like this:
var cmd = System.Diagnostics.Process.Start("myApp.exe");
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("MyName"); // this should be entered in console as username
So I specify username from my code, instead of writing it to console manually. Code above is not working for me. Is there a way to do this?
enter username
You're first starting the Process and then you're manipulating the startup arguments. You need to start the process afterwards.
use something like this instead:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = false; //required to redirect standart input/output
// redirects on your choice
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.FileName = ...app path to execute...;
startInfo.Arguments = ...argumetns if required...;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.StandardInput.WriteLine(...write whatever you want...);
source
You need redirect standard input. Create ProcessStartInfo structure, then start a process and after process started write to the process standart input
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = pathToApplication,
RedirectStandardInput = true,
UseShellExecute = false
};
Process process = Process.Start(processStartInfo);
var writer = process.StandardInput;
writer.WriteLine("MyName");
Console.ReadLine();

System.ComponentModel.Win32Exception c#

This is my code:
string damnfile = System.IO.Path.GetDirectoryName(choofdlog.FileName);
ProcessStartInfo startInfo = new ProcessStartInfo("% ProgramFiles %\\Windows Defender\\MpCmdRun.exe");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = "-Scan -ScanType 3 -File" + damnfile;
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
As you can see, I want to run WinDefender scan for a specific file, but it throws error that the process is not found, but this code works:
ProcessStartInfo startInfo = new ProcessStartInfo("netstat");
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.Arguments = "-a";
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
Also I want in the second code to redirect the cmd output to listBox, but thats the second problem, first help me with the defender scan please.
Solveded like now its opening, because I replaced
ProcessStartInfo startInfo = new ProcessStartInfo("% ProgramFiles %\\Windows Defender\\MpCmdRun.exe");
with
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Program Files\\Windows Defender\\MpCmdRun.exe");
Now appeared another problem: even I passed some arguments, the windows immediately close.

c# Process exe with arguments gives error

I sort of stuck into what should be a small issue. I need to run two different processes but the second failures.
My first process is:
string workingDirectory = "c:\\xxxx\\bin\\";
string fileName = "xxxx.xxxx.cmd";
string arguments = "-Sxxxx -f -d --port xxxx > c:\\xxxx\\tmp\\bug.log 2>&1";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = workingDirectory;
startInfo.FileName = fileName;
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
No worries here. Everything works but the second.
string workingDirectory = "c:\\xxxx\\bin\\";
string fileName = "WebDaemon.exe";
string arguments = "-debug -i WebDaemon.xxxx.1234 --ServerPort=4444 --MultipleServerPorts=1 --CGIPort=4500 --CGICallbackPort=4600 --MinServerProcs=2 --MaxServerProcs=2 > c:\\xxxx\\tmp\\WebDaemonTrace.log 2>&1";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = workingDirectory;
startInfo.FileName = fileName;
startInfo.Arguments = arguments;
Process process = new Process();
process.StartInfo = startInfo;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
Gives an error. The process created and shows up in taskmanager, but after 5 seconds it disappears. Furthermore i can read in a debug message that:
"commandline argument '>' is unknown"
I cannot seem to find the problem. Open to any suggestions :-)

how can I get list of active LAN user using ARP

in command prompt we can get list of ACTIVE user's IP [on LAN] using command like
arp - g
How can I get similar list using C#
You can use Process to execute commands from c# program
Try This:
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "arp";
startInfo.Arguments = "-g";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();
String strData = process.StandardOutput.ReadToEnd();

Categories