gpt4 book ai didi

c# - cmd 参数不起作用

转载 作者:行者123 更新时间:2023-11-30 20:08:12 34 4
gpt4 key购买 nike

我正在尝试从本地安装的 SQL Server 实例中获取所有服务器名称。我知道在 cmd 中有一个“sqlcmd -L”命令返回这个列表。以下是我用来运行 cmd 的代码。

Process sqlServers = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = false; (DEBUGGING PURPOSES)
psi.FileName = "cmd";
psi.Arguments = @"sqlcmd -L";
psi.UseShellExecute = false;
sqlServers = Process.Start(psi);
string serverList = sqlServers.StandardOutput.ReadToEnd();
sqlServers.WaitForExit(30000);

我在程序的另一部分中使用了这个代码库,它工作正常。虽然我没有读回一个值。

谁能帮我找出为什么当我运行此代码时会打开一个 cmd 窗口,但没有运行任何参数,也没有返回任何内容。

最佳答案

如果您希望通过“cmd.exe”调用命令,您需要指定“/c”参数,因此您的代码应该如下所示:

psi.FileName = "cmd.exe";
psi.Arguments = "/c sqlcmd -L"

尽管我不清楚您为什么通过“cmd.exe”调用它而不是直接使用“sqlcmd”。

关于c# - cmd 参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7314467/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com