I was trying to make a simple backup application to do some tasks like
- Rar folder to destination
- backup database files
- copy some files from another machine
I can do all those things in CMD, so I made an EXE that simple executes those commands in the CMD with Process.
The only one that simple don't work, and don't send any error messages is the database backup, IF I copy the genereted string and past in a CMD it works. So I have no idea why it is not working.
The process config, start, stop and output listner code:
private string lastLine;
private void Wait(string text) {
while (lastLine == null || !lastLine.Contains(text)) {
System.Threading.Thread.Sleep(500);
}
}
private Process cmd = null;
private void StartCmd() {
if (cmd != null) {
StopCmd();
}
ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe") {
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
Verb = "runas",
};
cmd = Process.Start(processStartInfo);
cmd.OutputDataReceived += CmdDataListner;
cmd.BeginOutputReadLine();
}
private void StopCmd() {
cmd.Close();
cmd.Dispose();
cmd = null;
}
private void CmdDataListner(object sender, DataReceivedEventArgs e) {
CmdListner(e.Data);
}
//This is just a RichText in the form to see the output
private void CmdListner(string text) {
if (text == null) {
return;
}
text = text.Trim();
if (text.Length == 0) {
return;
}
if (richTextBox1.InvokeRequired) {
richTextBox1.Invoke(new Action<string>(CmdListner), text);
} else {
lastLine = text;
richTextBox1.AppendText(text + Environment.NewLine);
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.ScrollToCaret();
}
}
The CMD commands execution:
private void Backup(){
cmd.StandardInput.WriteLine("D:");
cmd.StandardInput.WriteLine("cd " + path);
cmd.StandardInput.WriteLine("\"c:\\Program Files (x86)\\WinRAR\"\\rar.exe a File.rar targetFolder\"");
Wait("Done");
//More commands...
//Everything works fine so far...
//Than I try
cmd.StandardInput.WriteLine("SqlCmd -E -S ServerName\\Instance –Q \"BACKUP DATABASE BaseXYZ TO DISK = 'D:\\backups\\BaseXYZ.bak'\"");
//Nothing more happens, no output, not error, no nothig...
Wait("BACKUP DATABASE successfully");
cmd.StandardInput.WriteLine("SqlCmd -E -S ServerName\\Instance –Q \"BACKUP DATABASE BaseXYZxxx TO DISK = 'D:\\backups\\BaseXYZxxx.bak'\"");
Wait("BACKUP DATABASE successfully");
cmd.StandardInput.WriteLine("exit");
cmd.StandardInput.Close();
cmd.WaitForExit();
StopCmd();
Application.Exit();
}
Sure that I have way more commands then on this block, lots of for loops, etc. But everything works, only the Database backup that don't work, even in isolated test. It simple hangs like if it was waiting for more inputs
I have tested it like
string commandLine = sqlBackup;
MessageBox.Show(commandLine);
cmd.StandardInput.WriteLine(commandLine);
You just press CTRL + C in the Alert box to copy it. Than past it in the CMD and works, the backup take less then 1sec. The file is there in the folder.
When executed by the Process it just hangs, it does not create any file, even if I wait way more than 1sec.