【问题标题】:Command working in command prompt doesn't work in C#在命令提示符下工作的命令在 C# 中不起作用
【发布时间】:2020-08-04 01:52:56
【问题描述】:

我正在尝试使用 C# 命名空间 System.Diagnostics 运行命令。

我使用这些命令来安装 SQL Server 和 SQL Server Management Studio。

对于 SQL Server,我从:https://www.microsoft.com/en-us/download/confirmation.aspx?id=55994 下载了安装程序并安装了基本版本。

之后我想用这个:

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)
{
}

此命令适用于cmd.exe:

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

我从https://download.microsoft.com/download/D/D/4/DD495084-ADA7-4827-ADD3-FC566EC05B90/SSMS-Setup-ENU.exe下载了 SQL Server Management Studio 的安装程序

然后运行:

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();

通过 cmd:

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

cmd.exe 中的命令有效。在 Visual Studio 中它没有。我以管理员权限打开 Visual Studio。在两种情况下,它永远不会从

返回
output = p.StandardOutput.ReadToEnd();

SQL Server 安装文档: https://docs.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 安装: https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?redirectedfrom=MSDN&view=sql-server-ver15

【问题讨论】:

  • 在参数前加上“/K”,看看是否有错误。 /K 即使在命令完成后也保持控制台打开
  • 您的代码看起来像一个单词宏。尝试直接启动SSMS-Setup-ENU.exe,不使用所有外部cmd并启动。

标签: c# sql-server cmd command-line


【解决方案1】:

我认为这是因为您在论证中使用的不是“/C”而是“开始”。

和 CMD 行有点不同

试试这样吧:

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

【讨论】:

  • "\\EkGrafConveyors.d
  • 也许你最好仔细看看你逃跑的角色?
猜你喜欢
  • 2012-11-15
  • 2018-06-20
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 2010-12-13
  • 2013-07-13
相关资源
最近更新 更多