2

I am trying to get all of the server names from the installed instances of SQL Server locally. I know that there is a command of "sqlcmd -L" that returns this list in cmd. The following is the code that I am using to run a the 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);

I have used this code base in another section of the program, and it works fine. Although I am not reading back a value.

Can someone please help me find why when I run this code that a cmd window opens, but no arguments are run and nothing is returned.

Seige
  • 304
  • 5
  • 27
  • 1
    I would expect `sqlcmd` to be the program, and `-L` to be the arguments. Is it that you are hoping to get `sqlcmd` for free without specifying the path? – Kirk Woll Sep 06 '11 at 02:55
  • 1
    You forgot to use the `/c` flag. See http://stackoverflow.com/questions/3616010/start-command-windows-and-run-commands-inside – Raymond Chen Sep 06 '11 at 02:59
  • The /c will be inserted in there when it is working. but that is the lease of the proble :) Kirk has the right idea. Thanks. This was insanly quickly answered. – Seige Sep 06 '11 at 03:17

3 Answers3

4

If you wish to invoke a command through "cmd.exe" you need to specify the "/c" argument, so you code should look like:

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

Although it's unclear to me why you are invoking this through "cmd.exe" rather than just using "sqlcmd" directly.

Sven
  • 21,903
  • 4
  • 56
  • 63
  • This isnt a solution to the problem. All the /c does is close the cmd when the arguement is finished. I found the answer, and the psi.FileName has to be sqlcmd, and the arguement is -L. Thank you to Mark Hall for posting this also – Seige Sep 06 '11 at 03:16
  • 1
    @Seige That's not true. Without either /c or /k, cmd.exe will not execute the argument at all. Therefore this should solve your problem. However, using sqlcmd directly is indeed better, as I already pointed out. In any case, I'm glad you sorted it out. – Sven Sep 06 '11 at 03:24
  • Thanks Sven. I was to believe that I could still run the command without /c or /k. Although I did try this in the code before I asked here and it also failed to work. The only thing that did was the sqlcmd. Before here, I didnt know I could call sqlcmd as the FileName. – Seige Sep 06 '11 at 04:13
3

If you replace Cmd with the name of the program you wish to run and assign your arguments it will work.

psi.FileName = "sqlcmd"; 
psi.Arguments = "-L"; 
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
0

Adding "/c" before the command worked for me. I run a command in command line without showing the cmd window. You can use a path for the working directory if you want to run the command in a specific folder.

string path = Path.Combine(Directory.GetCurrentDirectory(), "folderInDebug");
var startInfo = new System.Diagnostics.ProcessStartInfo
{
      WorkingDirectory = path,
      FileName = "cmd",
      Arguments = "/c sysdm.cpl",
      UseShellExecute = false
 };
 Process proc = Process.Start(startInfo);

Without the "/c" for the argument, it did not start the system properties.