How can we use a command with pipe in C# - c#

I'm trying to run a command with a pipe that uses findstr.
Following is a simple example of the command. (In actual case my command is more complicated than this, using this command for explanation)
dir | findstr /i /c:"web"
following is the function I use to run the process in C#.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo()
{
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C dir | findstr /i /c:\"web\"",
RedirectStandardError = true,
RedirectStandardOutput = true
};
process.Start();
string sResponse = process.StandardOutput.ReadToEnd();
process.WaitForExit();
I haven't figured out how to run this command using this process.
Above will work for commands without | (Pipe)
Please let me know if there are any workarounds.

Related

mysqldump command not recognized when called from c#

I want to dump a mysql database through my c# app.
This is how the code looks like :
ProcessStartInfo cmd = new ProcessStartInfo()
{
FileName = "cmd.exe",
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
Arguments = "/C cd %programfiles%\\mysql\\mysql server 5.6\\bin\\ & mysqldump.exe --user=user --password=pwd --host=localhost --protocol=tcp --verbose=true --port=3306 --default-character-set=utf8 --skip-triggers \"db\" > \"mydb.sql\"",
UseShellExecute = false,
};
using (Process exeProcess = Process.Start(cmd))
{
exeProcess.WaitForExit();
}
But it says that "mysqldump" is not recognized as an internal command.
Note that when i execute the command in a normal command prompt windows, everything works fine.
I tried to put change the command llke this :
"/C cd /D %programfiles%\\mysql\\mysql server 5.6\\bin\\ & mysqldump.exe --user=user --password=pwd --host=localhost --protocol=tcp --verbose=true --port=3306 --default-character-set=utf8 --skip-triggers \"db\" > \"mydb.sql\""
But the execution seems to freeze my app for a way too long time.

C# Process is not stopping

I am using the below code C# Process instance to do a CSV import for PostgreSQL.
Process process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = #"cmd.exe",
Arguments = $"/c cat \"{filePath}\" | psql -h 127.0.0.1 -U {user} -d {dbname} -w -c \"copy data_temp from stdin csv header\" ",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Dispose();
It works but I look in task manager and after a while there is lot of PostgreSQL processes running and eventually the database gets locked.
Am I not closing the process down properly? How can I ensure it doesn't stay open?
Not to sure if this could help or not but here are two examples I have of killing processes via msiexec and Exe.
Uninstalling Msiexec.exe files…
Process x = new Process();
x.StartInfo.FileName = "msiexec.exe";
x.StartInfo.Arguments = "/qb /x {81681F4C-83F1-4F22-9AEB-C7DA7C372EA2}";[quiet uninstall]
x.Start();
x.WaitForExit();
and
Uninstalling exe (file path method, not msiexec)
Process a = new Process();
a.StartInfo.FileName = JPRO_8_5_0; //defined in a string before hand.
a.StartInfo.Arguments = "/uninstall /quiet";
a.Start();
a.WaitForExit();
You can try making the filename a string that is defined before instead of having it as #"cmd.exe", that might help!

Run "sw-precache" command from c#

I want to generate "service-worker.js" using sw-precache
from command line (cmd.exe) I run this command and output is :
Total precache size is about 145 kB for 35 resources.
service-worker.js has been generated with the service worker contents.
When I run this command from C# output is empty, here is my code:
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"cmd.exe",
Arguments = #"/c sw-precache",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WorkingDirectory = Path.GetFullPath(myDir)
}
};
proc.Start();
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd();
If I run command like "dir" in Arguments = #"/c dir", output contains message from dir command,
what I do wrong?
Edit:
In Console Application, output is returned normally, but not in Web Application
WaitForExit first then read the output. It stays after the process has exited.

Start process on Mac

I want to write command in Mac equivalent to "ffmpeg -i input.avi output.avi" in Windows. My code is:
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "ffmpeg.exe",
Arguments = "-i /Users/John/Desktop/input.avi /Users/John/Desktop/hhh.avi",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
After I write "mono previousCode.exe" in the terminal in Mac, I get "filename unknown" error.
Where is my mistake?
Executables in MacOS generally don't end with a .exe suffix, so just leave off the ".exe" and see if that works. If not, you'll need to provide the full path to ffmpeg

Start command windows and run commands inside

I need to start the command window with some arguments and run more commands inside.
For example, launch a test.cmd and run mkdir.
I can launch the test.cmd with processstartinfo , but i am not sure how to run further commands. Can I pass further arguments to the test.cmd process?
How do I go about this?
Unable to add comments to answer... SO writing here.
Andrea, This is what I was looking for. However the above code doesnt work for me.
I am launching a test.cmd which is new command environment (like razzle build environment) and I need to run further commands.
psi.FileName = #"c:\test.cmd";
psi.Arguments = #"arg0 arg1 arg2";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process p = new Process();
p.StartInfo = psi;
p.Start();
p.StandardInput.WriteLine(#"dir>c:\results.txt");
p.StandardInput.WriteLine(#"dir>c:\results2.txt");
You can send further commands to cmd.exe using the process
standard input. You have to redirect it, in this way:
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process {StartInfo = startInfo};
process.Start();
process.StandardInput.WriteLine(#"dir>c:\results.txt");
process.StandardInput.WriteLine(#"dir>c:\results2.txt");
process.StandardInput.WriteLine("exit");
process.WaitForExit();
Remember to write "exit" as your last command, otherwise the cmd process doesn't terminate correctly...
The /c parameter to cmd.
ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause");
Process.Start(start);
(pause is just an example of what you can run)
But for creating a directory you can do that and most other file operations from c# directly
System.IO.Directory.CreateDirectory(#"c:\foo\bar");
Start a cmd from c# is useful only if you have some big bat-file that you don't want to replicate in c#.
What are you trying to achieve? Do you actually need to open a command window, or do you need to simply make a directory, for example?
mkdir is a windows executable - you can start this program in the same way you start cmd - there's no need to start a command window process first.
You could also create a batch file containing all the commands you want to run, then simply start it using the Process and ProcessStartInfo classes you're already using.
How come this doesn't work?
var startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = false
};
var process = new Process { StartInfo = startInfo };
process.Start();
process.StandardInput.WriteLine(#" dir");
process.WaitForExit();

Categories

Resources