views:

72

answers:

4

I have created a .bat file that displays all the users logged in Windows Terminal Services. I can execute the .bat file in my c# code behind and display the results in plain text in a label or text box. What i would like to do is data bind the user name and the session ID in a 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;
        }

  }
A: 

You can create a DataTable in memory, add columns for username and sessionid. In your loop, split() the output string based on the separating char (comma? semicolon?) and add rows to your table based on that.

Then set that to datasource of the grid and databind.

Alternatively, you can use other types of collections.

MCain
A: 

You could create an xml structure and then bind the data grid to that structure.

yamspog
+1  A: 

Rather than depend on running a .bat file, why don't you do it directly using something like the Remote Desktop Services API?

http://msdn.microsoft.com/en-us/library/aa383459%28VS.85%29.aspx

Or maybe using Wmi:

http://gallery.technet.microsoft.com/ScriptCenter/en-us/f053de86-f053-474b-9b21-f2e6b161948f

RQDQ
Some sample code from SO:http://stackoverflow.com/questions/132620/how-do-you-retrieve-a-list-of-logged-in-connected-users-in-net
RQDQ
+2  A: 

You could 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'll want to:

  • do some error checking (I made a lot of assumptions in the sample)
  • make sure the user name is before session ID
  • also make sure you have a DataGrid (myGridView) to bind to
  • Check that your output is indeed newline and comma delimited
  • If not, then update the chars in code
  • Also... i made this less efficient on purpose to show the process
Paul Sasik
@Paul Sasik I need to dataBind() the output.
djshortbus
The line myGridView.DataSource = table; should do that for you. If it doesn't happen alone, you may have to call myGridView.BindData (or DataBind) right after that line. (i was assuming WinForms but judging from your past Qs you might be in ASP.NET - in which case, you do need to call the BindData method explicitly.)
Paul Sasik