Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 986141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:16:22+00:00 2026-05-16T05:16:22+00:00

I have created a .bat file that displays a servers terminal Service SessionID and

  • 0

I have created a .bat file that displays a servers terminal Service SessionID and Username.
Im Displaying the Information in a datagrid

Here is the output of the .bat file:

C:\Documents and Settings\adcock>qwinsta /server:ilsap01
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 console                                     0  Conn    wdcon
 rdp-tcp                                 65536  Listen  rdpwd
                   Jrodriguez               27  Disc    rdpwd
                   pbahena                   8  Disc    rdpwd
                   tfurr                     3  Disc    rdpwd
 rdp-tcp#2187      kchild                   14  Active  rdpwd
                   Trhodes                  10  Disc    rdpwd
                   ajordan                  16  Disc    rdpwd
                   Trhodes                  11  Disc    rdpwd
 rdp-tcp#2191      rluna                    15  Active  rdpwd
 rdp-tcp#2192      lcathey                  17  Active  rdpwd

the only information i want to display is the SessionID and Username that works somewhat with the code below.

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 rawUserData = listFiles.StandardOutput;
    listFiles.WaitForExit(30000);
    try
    {

        DataTable table2 = new DataTable();
        table2.Columns.Add(new DataColumn("UserName", typeof(string)));
        table2.Columns.Add(new DataColumn("SessionId", typeof(string)));

        String myString = rawUserData.ReadToEnd();
        string exp = @"([\w_]+)"; ;


        MatchCollection matches = Regex.Matches(myString, exp,RegexOptions.IgnoreCase);

        IEnumerator en = matches.GetEnumerator();

        while (en.MoveNext())
        {
            Match match = (Match)en.Current;

            if (en.Current.ToString() == "rdpwd")
            {
                en.MoveNext();

                Match match_Item = (Match)en.Current;

                string item = match_Item.Value;

                en.MoveNext();

                Match match_Item2 = (Match)en.Current;

                string item2 = match_Item2.Value;

                DataRow row = table2.NewRow();
                row[0] = item.Split()[0];
                row[1] = item2.Split()[0];
                //row[1] = item.Split(',')[1];
                table2.Rows.Add(row);

                //en.MoveNext();
                //break;

            }


        }

        this.displayUsers.DataSource = table2;
        this.displayUsers.DataBind();
    }

    catch (Exception ex)
    {
        Response.Write(ex);
    }

The Error i get is:
Enumeration has either not started or has already finished.

I set a break point and it seams that the while loop completes but starts over and after it adds a couple duplicate records it throws the error. any ideas on whats be causing this. Im thinking its my RegEx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:16:23+00:00Added an answer on May 16, 2026 at 5:16 am

    You are calling MoveNext twice inside the loop. That is causing the problem.

    When you are in the last row, you call MoveNext, the it finds “rdpwd”, then it calls MoveNext one time, then it calls it again, and this is when the enumerator becomes invalid.
    In then last MoveNext in the while it breaks because it’s already at the end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a .bat file that displays all the users logged in Windows
I have created below bat file to run my RMI server @echo Off set
I need to capture the output created by a .bat file that is run
i have a batch(backup.bat)file.in that i have written a command to create a backup
I have created a batch file for ex(ex.bat) Is it possible to run batch
I have created a jar which needs to be called in a bat file.
I want to create bat file that will install windows service, passed as a
I have a bunch of dynamically created *.BAT files. These BAT files are used
I have created some JQuery that will expand a div 'popup' on hover and
I have created an android application that calls (using kSOAP library) a SOAP based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.