mysqldump command not recognized when called from c# - 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.

Related

How can we use a command with pipe in 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.

How to create a C# application that opens "cmd.exe" in another window?

What I'd like to Do
I'd like to create a simple C# application that creates a Process object (the application's child process) that runs cmd.exe and, inside that shell, execute the command echo "Hello World!" (or whatever arbitrary string I specified before compiling the application). The C# application, when built and ran, creates and leave the shell in this state:
Attempts
I've searched stackoverflow and MSDN for examples but it's difficult to find the right options to set for Process, ProcessStartInfo. In particular, I tried:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
/*
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 = "echo helloworld!";
string strCmdText;
process.StartInfo = startInfo;
process.Start();
*/
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"cmd.exe", // iexplorer.exe opened up ie!
Arguments = "",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = false,
WorkingDirectory = #"C:\Users\mtran\Desktop\ConsoleApp1",
WindowStyle = ProcessWindowStyle.Normal
}
};
proc.Start();
}
}
}
but either second cmd window (for the child process) never appears (if I set RedirectStandardOutput = false)or the output of from the child process gets written to the parent's cmd window.
Try this:
Process.Start("cmd.exe", "/k ECHO hello Juan!");
It will launch the command window, execute the ECHO statement and keep the window open.
Feel free to use a ProcessStartInfo instance as the parameter if you need additional configuration.
If you run cmd /? on a command prompt, you can see additional information about the switches:
/C Carries out the command specified by string and then
terminates
/K Carries out the command specified by string but remains

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