Silny ToJa
Silny ToJa

Reputation: 2261

Command working in command prompt doesn't work in C#

I am trying to run a command using the C# namespace System.Diagnostics.

I use these commands to install SQL Server and SQL Server Management Studio.

For SQL Server, I downloaded the installer from: https://www.microsoft.com/en-us/download/confirmation.aspx?id=55994 and installed the basic version.

After that I want to use this:

try
{
    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.FileName = "cmd.exe";

    p.StartInfo.Arguments = "C:\\SQLServer2017Media\\Express_ENU\\SETUP.exe /Q /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /FEATURES=SQL,AS,IS,Tools /INSTANCENAME=MSSQLSERVER";

    p.Start();
    string output = p.StandardOutput.ReadToEnd();
}
catch (Exception e)
{
}

This command works in cmd.exe:

C:\SQLServer2017Media\Express_ENU\setup.exe /Q /IACCEPTSQLSERVERLICENSETERMS 
    /ACTION=install /FEATURES=SQL,AS,IS,Tools /INSTANCENAME=MSSQLSERVER

I downloaded the installer for SQL Server Management Studio from https://download.microsoft.com/download/D/D/4/DD495084-ADA7-4827-ADD3-FC566EC05B90/SSMS-Setup-ENU.exe

and run:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "start \"\" " + DownloadsFolderPath + "\\SSMS-Setup-ENU.exe /install /Passive SSMSInstallRoot=C:\\Program Files\\Microsoft SQL Server /norestart";
p.Start();
string output = p.StandardOutput.ReadToEnd();

By cmd:

start "" C:\Users\...\Downloads\SSMS-Setup-ENU.exe /install /Passive 
         SSMSInstallRoot=C:\Program Files\Microsoft SQL Server /norestart

The command in works in cmd.exe. In Visual Studio it doesn't. I open Visual Studio with administrator privileges. In two cases it never returns from

output = p.StandardOutput.ReadToEnd();

Documentation for SQL Server installation: https://learn.microsoft.com/en-us/sql/database-engine/install-windows/install-sql-server-from-the-command-prompt?view=sql-server-ver15#Feature

SQL Server Management Studio installation: https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?redirectedfrom=MSDN&view=sql-server-ver15

Upvotes: 2

Views: 419

Answers (1)

Thomas Sportouch
Thomas Sportouch

Reputation: 96

I think it's because you are not using "/C" in your argument but a "start".

It's a little diffrent from CMD line

try like that instead:

p.StartInfo.Arguments = "/C \"\" " + DownloadsFolderPath + "\\SSMS-Setup-ENU.exe /install /Passive SSMSInstallRoot=C:\\Program Files\\Microsoft SQL Server /norestart";

Upvotes: 4

Related Questions