1

I need to launch a command from the prompt with parameters within my WPF C# application to synchronize two databases.

The command in the file.bat is as follows:

"c:\Program Files\Microsoft SQL Server\100\COM\replmerg.exe" -Publisher [sql2008-srv\sql2008srv] -PublisherDB [MIODB] -Publication [DBPublication] -Subscriber [UTENTE\SQL2008R2EXP] -SubscriberDB [MIODB] -SubscriptionType 1 -SubscriberSecurityMode 1 -Distributor [sql2008-srv\sql2008srv] -OutputVerboseLevel 4 -Output C:\Windows\Temp\mergeagent.log 

Below my code but does not run. Just open the shell with the flashing cursor:

string ciao = "\"c:\\Program Files\\Microsoft SQL Server\\100\\COM\\replmerg.exe\" -Publisher [sql2008-srv\\sql2008srv] -PublisherDB [DataNavi] -Publication [DataNaviPublication] -Subscriber [SFRANCESCO\\SQL2008R2EXP] -SubscriberDB [DataNavi] -SubscriptionType 1 -SubscriberSecurityMode 1 -Distributor [sql2008-srv\\sql2008srv] -OutputVerboseLevel 4 -Output C:\\Windows\\Temp\\mergeagent.log ";

try
{
    Process.Start("CMD.exe", "/K" +ciao);
}
catch (Exception ex)    
{        
    // Implement appropriate error handling here.
    MessageBox.Show("errore:" + ex.Message, "Errore sincronizzazione");
}
Member2017
  • 431
  • 2
  • 8
  • 16
Disaji
  • 89
  • 2
  • 11

1 Answers1

5

There are few ways to deal with this

1. Use cmd.exe /C command instead of the /K option.

Excerpt from the cmd.exe /? help

/C Carries out the command specified by string and then terminates
/K Carries out the command specified by string but remains

string ciao = @"""c:\Program Files\Microsoft SQL Server\100\COM\replmerg.exe"" "+
    @"-Publisher [sql2008-srv\sql2008srv] -PublisherDB [DataNavi] -Publication [DataNaviPublication] "+
    @"-Subscriber [SFRANCESCO\SQL2008R2EXP] -SubscriberDB [DataNavi] -SubscriptionType 1 "+
    @"-SubscriberSecurityMode 1 -Distributor [sql2008-srv\sql2008srv] -OutputVerboseLevel 4 "+
    @"-Output C:\Windows\Temp\mergeagent.log";

try {
    var proc = Process.Start("CMD.exe", "/c " + ciao);
    proc.WaitForExit(); // optionally wait for exit
} catch (Exception ex) {
    // Implement appropriate error handling here.
    Console.WriteLine("error:" + ex.Message);
}

2. Use batch file

With such a long argument list, you might just run into issues with quotes (if not now, then in future). A better way is to let the command stay in the batch file and execute the batch file.

type file.bat
"c:\Program Files\Microsoft SQL Server\100\COM\replmerg.exe" -Publisher [sql2008-srv\sql2008srv] -PublisherDB [MIODB] -Publication [DBPublication] -Subscriber [UTENTE\SQL2008R2EXP] -SubscriberDB [MIODB] -SubscriptionType 1 -SubscriberSecurityMode 1 -Distributor [sql2008-srv\sql2008srv] -OutputVerboseLevel 4 -Output C:\Windows\Temp\mergeagent.log

var proc = Process.Start("file.bat");

3. Run the command directly without cmd.exe

string ciao = @"-Publisher [sql2008-srv\sql2008srv] -PublisherDB [DataNavi] -Publication [DataNaviPublication] " +
    @"-Subscriber [SFRANCESCO\SQL2008R2EXP] -SubscriberDB [DataNavi] -SubscriptionType 1 " +
    @"-SubscriberSecurityMode 1 -Distributor [sql2008-srv\sql2008srv] -OutputVerboseLevel 4 " +
    @"-Output C:\Windows\Temp\mergeagent.log";

var proc = Process.Start(@"c:\Program Files\Microsoft SQL Server\100\COM\replmerg.exe", ciao);
Vikhram
  • 4,294
  • 1
  • 20
  • 32