简体   繁体   中英

How to display a multi-line output from cmd in c#

I need my c# program to run a cmd query to show all sql services and then display them in a message box. In the situation I will be running this in, there will frequently be more than one and the names may be different each time.

"Wmic service where (PathName like '%Binn\\sqlservr%') get caption, name" displays the information I need but there are multiple lines returned. the WriteNote() method is one that writes the information to a textbox in the program.

I have tried everything I can find on Google and nothing seems to be working.

    private void DoListSQLServices()
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "Wmic service where (PathName like '%Binn\\sqlservr%') get caption, name";
        process.StartInfo = startInfo;
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false; 
        process.Start();
        string output = process.StandardOutput.ReadToEnd();

          this.WriteNote(output);
        process.WaitForExit();             
    }

When I run this code I get this:

Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.

{file location of project}\\bin\\Debug>

and a pbd file is put in the debug folder.

What I want is for it to show the caption and name of the SQL services like if you run that command through cmd manually. Ultimately the user will type one of them in for the program to restart them but I would rather have the program display the list than have the user go to Services in windows and search for them manually.

Thanks in advance!

As mentioned by Noodles in the comments, it's much better to do this via the WMI support in .NET. One of the fairly direct methods for this is to use the ManagementObjectSearcher class to execute an ObjectQuery against the WMI data.

Create a new console application. Add a reference for the System.Management assembly to and add using System.Management; to the top of your program.cs file.

Now add the following code:

static void Main()
{
    // Create a scope (connection to WMI)
    var scope = new ManagementScope(@"\\localhost\root\cimv2");

    // Create query
    var query = new ObjectQuery(@"SELECT Name,Caption FROM Win32_Service WHERE PathName like '%Binn\\sqlserv%'");

    // Create a search to run the query against the scope
    using (var search = new ManagementObjectSearcher(scope, query))
    {
        // Iterate through the query results
        foreach (var item in search.Get())
        {
            // get values, all strings in this case
            string name = (string)item["Name"];
            string caption = (string)item["Caption"];
            Console.WriteLine("{0}\t{1}", name, caption);
        }
    }
}

Note that the ObjectQuery query syntax is a little different to the WMIC syntax. It's called WQL (WMI Query Language) and is heavily modelled on SQL.

Hopefully you can adapt the above for your own use fairly easily. Just be careful with the actual type of the fields you're fetching. Here's a list of properties for Win32_Service objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM