C # .net Populating datagrid from result of .bat file

I created a .bat file that displays all users registered in Windows Terminal Services. I can execute the .bat file in my C # code and display the results as plain text in a shortcut or text box. I would like the data to bind the username and session ID in the data grid.

  protected void Button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
    psi.RedirectStandardOutput = true;
    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    psi.UseShellExecute = false;
    System.Diagnostics.Process listFiles;
    listFiles = System.Diagnostics.Process.Start(psi);
    System.IO.StreamReader myOutput = listFiles.StandardOutput;
    listFiles.WaitForExit(2000);
     if (listFiles.HasExited)
       {
        string output = myOutput.ReadToEnd();
        // this.TextBox1.Text = output;
        }

  }
+3
source share
4 answers

You can try something like this:

        string[] input = myOutput.ReadToEnd().Split('\n');

        DataTable table = new DataTable();

        table.Columns.Add(new DataColumn("UserName", typeof(string)));
        table.Columns.Add(new DataColumn("SessionId", typeof(string)));

        foreach (string item in input)
        {
            DataRow row = table.NewRow();
            row[0] = item.Split(',')[0];
            row[1] = item.Split(',')[1];
            table.Rows.Add(row);
        }

        myGridView.DataSource = table;

        // for WebForms:
        myGridView.DataBind();

Of course you want:

  • do some error checking (I made a lot of assumptions in the sample)
  • make sure the username is before the session id
  • also make sure you have a DataGrid (myGridView) to bind to
  • , .
  • ,
  • ...
+2

DataTable , sessionid. split() char ( ?) .

.

.

0
source

You can create an xml structure and then bind a data grid to that structure.

0
source

Source: https://habr.com/ru/post/1757384/


All Articles