Why is dumping MySQL database programmatically different from dumping via command line? - c#

To dump database from command line, all I need to do is:
mysqldump -uroot --password= myDb --routines> "C:\s.sql"
So all I would try programmatically is this, which is the direct interpretation of it I suppose:
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = "-uroot --password= myDb --routines> \"C:\\s.sql\"";
Process process = Process.Start(psi);
process.WaitForExit();
process.Close();
Which doesn't work at all. Instead I have to go for this which one can find all over the net, which works too.
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = dumpUtilityPath;
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.Arguments = string.Format("-R -u{0} --password={1} -h{2} {3} --routines", "root", "", "localhost", "myDb");
Process process = Process.Start(psi);
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
using (StreamWriter writer = new StreamWriter("C:\\s.sql"))
{
writer.WriteLine(output);
writer.Close();
}
Why is that I need to use stream writer to get the database in an sql file which I can do otherwise directly from commands in command prompt?
What is the role of -R in the second block?

You can not redirect stdout using ">" in arguments because that is a feature of the command prompt.
-R includes the stored procedures and functions in the dump. See http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html#option_mysqldump_routines for more information.

What you are doing in the command line version is using the shell to pipe the standard output to a file (the > command, followed by a file name, is a shorthand way of saying "take all of the standard output of this program and write it to this file"). To do the same thing from C#, you need to hand the standard output yourself and write it to a file.
The -R in the second example seems duplicative. According to this page, it is the same as --routines. Have you tried it without?

I thought I would include what the Arguments could look like programmatically, in our case we also wanted to dump the events of the DB to a file.
psi.Arguments = string.Format(#"-u{0} -p{1} -h{2} {3}", "someUser", "xxxxxx", "localhost", dbName, "--routines","--events");

Related

c# run .bat file application as administrator do not start

I have to run a .bat file from c#...
I use this method.
file = "C:\\Diego\\PublishCore\\Startup_service.bat";
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = true;
psi.FileName = file;
psi.UseShellExecute = true;
psi.Verb = "runas";
Process.Start(psi);
.BAT is executed... but the action I ask to perfom it does not execute...
If my .bat says MKDir MyDir... Its creates a Directory called MyDIr with no problems.
But when my bat says dotnet myApp.dll, a cmd Windows opens and closes, but it does not start myApp aplication....
If a doublé-click my .bat is runs fine.
What I am missing? Why the aplication does not start?
I solved it...
The problem was that, as my bat run the instruction dotnet myApp.dll.
I set the path file where the file was, but it was executed in the location where the my Solution is, instead of running in the same directory where I have .bat file.
I have to set WorkingDirectory and Arguments
C:\\Diego\\PublishCore\\Startup_InomCore.bat
ProcessStartInfo psi = new ProcessStartInfo();
psi.WorkingDirectory = "C:\\Diego\\PublishCore";
// psi.CreateNoWindow = true;
psi.FileName = #"cmd.exe";
psi.Arguments = "/c start /wait " + "C:\\Diego\\PublishCore\\Startup_InomCore.bat";
// psi.UseShellExecute = true;
psi.Verb = "runas";
var process = Process.Start(psi);

How do I get the output from handbrakecli --scan?

I am working on a c# program to loop over my Windows Media Center recorded TV shows (.wtv) and convert them using the handbrake cli. I just got everything to work now and I wanted to also utilize the --scan function so that I can customize the audio and video arguments based on the input file rather then set a static.
This is what I have so far for the scan but I can't seem to find where the data is that prints out to the console window.
var p = new Process();
var pSI = new ProcessStartInfo();
pSI.RedirectStandardOutput = true;
pSI.UseShellExecute = false;
pSI.FileName = HandBrakeLocation;
pSI.Arguments = string.Concat(#"--scan -i ", '"', inputFile, '"');
pSI.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo = pSI;
p.Start();
var stdout = p.StandardOutput;//streamreader
p.WaitForExit();
I thought that perhaps the p.StandardOutput would send the console output to the stdout StreamReader variable, but I could not find it anywhere inside the object. What am i missing?
Thanks for you time and assistance.
You can read the program output using the StandardOutput property of the process (which is a stream):
var output = stdout.ReadToEnd();
p.WaitForExit();
More info can be found on MSDN: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx

Unity c# run shell script

Using Unity3D and from editor script trying to run a script in the terminal on osx.
When running test.sh from terminal the GDCL application does its thing and then outputs the arguments. But if I run the script from Unity3D editor I only get the arguments in the output. GDCL doesn't run.
How can I get Unity3D to run terminal scripts?
C# script that runs test.sh (gives only output)
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Application.dataPath+"/test.sh";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "arg1 arg2 arg3";
//psi.Arguments = "test";
Process p = Process.Start(psi);
string strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
UnityEngine.Debug.Log(strOutput);
The test.sh script has chmod 777 (GDCL works only from terminal)
#!/bin/sh
GDCL ~/Documents/Unity/testproject/Assets/Font\ Normal.GlyphProject ~/Documents/Unity/testproject/Assets/Textures/fontNormal/font -fo PlainText-txt
for arg in $*
do
echo $arg
done
Try setting UseShellExecute to true or try running your shell directly and passing the script as the first argument.
psi.UseShellExecute = true;
Or
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "/bin/sh";
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Arguments = Application.dataPath + "/test.sh" + " arg1 arg2 arg3";
Don't forget to import:
using System.Diagnostics;
try which GDLC in terminal, get the full path and instead of GDLC in test.sh use full path then it will work

Run "tf.exe status" in C# and save the result

I´m trying to create a small console app in c#. I want to run the program and save all pending changes in TFS to a .txt file. But I cant get the arguments to work. Can someone help me?
Here is my code i haved done so far:
string argument = "#tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt";
try
{
Process process = new Process();
process.StartInfo.Arguments = "#call" + " " + "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\Common7\\Tools\\VsDevCmd.bat";
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Verb = "runas";
process.StartInfo.Arguments = argument;
process.StartInfo.CreateNoWindow = false;
process.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
aI'm not really sure that I understand what you're trying to call, exactly.
Let's assume you want to run the following command line from a C# application, as if you would call it from a command line:
tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt"
I would use this code:
string arguments = #"/C tf.exe status /collection:http://tiffany:8080/tfs/ /user:* /format:detailed >c:\\Status\\Detailed.txt";
this.process = new Process();
this.process.StartInfo.FileName = #"cmd.exe";
this.process.StartInfo.Arguments = arguments;
this.process.Start();
Edit:
If that's all your console app does, why not consider creating a batch (.BAT / .CMD) file instead of a C# application?
Instead of running a command line tool you could leverage the TFS API.
There are many articles out there, e.g. Code project article on topic
and
Sample code directly from the MSDN
I suppose you have to read standard error and output from process started:
Process process = new Process();
process.StartInfo.Arguments = #"status PATH /recursive";
process.StartInfo.FileName = "tf.exe";
process.StartInfo.CreateNoWindow = false;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
var st = process.StandardOutput.ReadToEnd();
var err = process.StandardError.ReadToEnd();
But parsing tf output is not easy and I'd like to suggest to use TFS API as #Mare said
You do not need to create an application in C # to save in a text file. Just use the parameters (...) > [file name].txt at the end of the command.
The ">" symbol send the result of any command to a file.

command prompt c# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirect Standard Output Efficiently in .NET
Capturing console output from a .NET application (C#)
I know how to execute something like this:
SomeEXE inputfile.txt
in the command prompt via C#.
The problem I am having is that SomeEXE opens another command prompt where it writes the outputs given inputfile.txt.
Is it generally possible to obtain these outputs? Thanks.
Here is my current code:
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 = "/C SomeEXE inputfile.txt";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
process.StartInfo = startInfo;
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
String line = outputReader.ReadToEnd();
ProcessStartInfo processStartInfo = new processStartInfo("SomeEXE", "inputfile.txt");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
// Here is where you grab the output:
processStartInfo.RedirectStandardOutput = true;
Process process = new Process {
StartInfo = processStartInfo
};
process.Start();
// Now use streams to capture the output
StreamReader outputReader = process.StandardOutput;
process.WaitForExit();
Now you can read the outputStream as necessary.
I am guessing this is what you mean. Also, here are the docs on RedirectStandardOutput
Also, if you know the path to the file that was generated (assuming the SomeEXE wrote to another file) you can use File.Open to access its contents after SomeEXE has executed (remember to wait until after otherwise SomeEXE may still have a handle on the file making it difficult to read it).

Categories