Background requirements: Automatic synchronization of data between applications, requiring high efficiency without human intervention
Technical implementation: C # start cmd, pass the necessary parameters via bcp command, achieve data export
<summary>
CMD, launch the application command
Do not show Command window
</summary>
<param name= "Programexepath" > Specify the full path of the application </param>
<param name= "programparmstr" > Execute command Line arguments </param>
public bool Startcmd (string Programexepath, String programparmstr)
{
BOOL result = FALSE;
Process Mypro = new process ();
Try
{
using (Mypro)
{
If there are spaces in the calling program path, the cmd command fails to execute and can be enclosed in double quotes, where two quotation marks indicate a quotation mark (escape)
String str = "";
if (programexepath.trim () = = "")
str = string. Format (@ "{0} {1}", Programparmstr, "&exit");
Else
str = string. Format (@ "" "{0}" "{1} {2}", Programexepath, Programparmstr, "&exit");
ProcessStartInfo startinfo = new ProcessStartInfo ();
Set the command to be executed
Startinfo.filename = "cmd.exe";
startinfo.arguments = str;
Booting without using the system shell
Startinfo.useshellexecute = false;
REDIRECT Input
Startinfo.redirectstandardinput = true;
REDIRECT Output
Startinfo.redirectstandardoutput = true;
Startinfo.redirectstandarderror = true;
Do not create a window
Startinfo.createnowindow = true;
Mypro.startinfo = StartInfo;
Mypro.start ();
MyPro.StandardInput.WriteLine (str);
MyPro.StandardOutput.ReadToEnd ();
MyPro.StandardInput.AutoFlush = true;
MyPro.StandardOutput.ReadToEnd ();
Mypro.waitforexit ();
result = true;
}
}
catch (Exception er)
{
Mypro.close ();
throw new Exception ("Error 006:" +er. Message);
}
return result;
}
C # cmd BCP export data