The bit of code below is giving me the error: Cannot read from a closed TextReader.

public static int maxUsers(int logMinute)
    {        
        StreamReader maxNum = new StreamReader(@"C:\Users\jplishka\Desktop\LoginSorted.csv");

        string sortedstrline = "";
        string sortedUser = null;
        string sortedComputer = null;
        string sortedDate = null;
        string sortedInTimeSec = null;
        string sortedOutTimeSec = null;
        string sortedInTime = null;
        string sortedOutTime = null;

        string[] sortedvalue = null;

        double loginSec;
        double logoutSec;
        double minTime;
        double maxTime;
        int maxLogins = 0;

        while (!maxNum.EndOfStream)
        {
            sortedstrline = maxNum.ReadLine();
            sortedvalue = sortedstrline.Split(',');

            sortedUser = sortedvalue[0].ToString();
            //Console.WriteLine("s--> {0}", sortedUser);

            sortedComputer = sortedvalue[1].ToString().TrimStart();
            //Console.WriteLine("s--> {0}", sortedComputer);

            sortedDate = sortedvalue[2].ToString().TrimStart();
            //Console.WriteLine("s--> {0}", sortedDate);

            sortedInTimeSec = sortedvalue[3].ToString().Trim();
            //Console.WriteLine("s--> {0}", sortedInTimeSec);
            loginSec = Convert.ToDouble(sortedInTimeSec);

            sortedOutTimeSec = sortedvalue[4].ToString().Trim();
            //Console.WriteLine("s--> {0}", sortedOutTimeSec);
            logoutSec = Convert.ToDouble(sortedOutTimeSec);

            sortedInTime = sortedvalue[5].ToString().TrimStart();
            //Console.WriteLine("s--> {0}", sortedInTime);

            sortedOutTime = sortedvalue[6].ToString().TrimStart();
            //Console.WriteLine("s--> {0}", sortedOutTime);


            if ((logMinute > logoutSec) && (logMinute < loginSec))
                maxLogins++;

            maxNum.Close();
        }
        
        return maxLogins;
    }

Here is the code for the full program if that helps. The purpose is to take a log file and return the max number of users logged in. the log files is in the format:
action, user, computer, date, time

public class LoginSort
{

    public static int maxUsers(int logMinute)
    {        
        StreamReader maxNum = new StreamReader(@"C:\Users\jplishka\Desktop\LoginSorted.csv");

        string sortedstrline = "";
        string sortedUser = null;
        string sortedComputer = null;
        string sortedDate = null;
        string sortedInTimeSec = null;
        string sortedOutTimeSec = null;
        string sortedInTime = null;
        string sortedOutTime = null;

        string[] sortedvalue = null;

        double loginSec;
        double logoutSec;
        double minTime;
        double maxTime;
        int maxLogins = 0;

        while (!maxNum.EndOfStream)
        {
            sortedstrline = maxNum.ReadLine();
            sortedvalue = sortedstrline.Split(',');

            sortedUser = sortedvalue[0].ToString();
            //Console.WriteLine("s--> {0}", sortedUser);

            sortedComputer = sortedvalue[1].ToString().TrimStart();
            //Console.WriteLine("s--> {0}", sortedComputer);

            sortedDate = sortedvalue[2].ToString().TrimStart();
            //Console.WriteLine("s--> {0}", sortedDate);

            sortedInTimeSec = sortedvalue[3].ToString().Trim();
            //Console.WriteLine("s--> {0}", sortedInTimeSec);
            loginSec = Convert.ToDouble(sortedInTimeSec);

            sortedOutTimeSec = sortedvalue[4].ToString().Trim();
            //Console.WriteLine("s--> {0}", sortedOutTimeSec);
            logoutSec = Convert.ToDouble(sortedOutTimeSec);

            sortedInTime = sortedvalue[5].ToString().TrimStart();
            //Console.WriteLine("s--> {0}", sortedInTime);

            sortedOutTime = sortedvalue[6].ToString().TrimStart();
            //Console.WriteLine("s--> {0}", sortedOutTime);


            if ((logMinute > logoutSec) && (logMinute < loginSec))
                maxLogins++;

            maxNum.Close();
        }
        
        return maxLogins;
    }

    public static void findLogout(string userName, string computerName, string logDate, string loginTime)
    {
        
        StreamReader copylog = new StreamReader(@"C:\Users\jplishka\Desktop\Temp_Log.csv");        

        string copystrline = "";
        string copylogAction = null;
        string copylogUser = null;
        string copylogComputer = null;
        string copylogDate = null;
        string copylogTime = null;
        string[] copyvalue = null;

        double loginSec;
        double logoutSec ;

        //Console.ReadLine();
        while (!copylog.EndOfStream)
        {            
            copystrline = copylog.ReadLine();
            copyvalue = copystrline.Split(',');

            copylogAction = copyvalue[0].ToString();
            //Console.WriteLine(copyvalue[0].ToString().TrimStart());
            //Console.WriteLine("c--> {0}", copylogAction);

            copylogUser = copyvalue[1].ToString().TrimStart();
            //Console.WriteLine(copyvalue[1].ToString().TrimStart());
            //Console.WriteLine("c--> {0}", copylogUser);

            copylogComputer = copyvalue[2].ToString().TrimStart();
            //Console.WriteLine(copyvalue[2].ToString().TrimStart());
            //Console.WriteLine("c--> {0}", copylogComputer);

            copylogDate = copyvalue[3].ToString().TrimStart();
            //Console.WriteLine(copyvalue[3].ToString().TrimStart());
            //Console.WriteLine("c--> {0}", copylogDate);

            copylogTime = copyvalue[4].ToString().TrimStart();
            //Console.WriteLine(copyvalue[4].ToString().TrimStart());
            //Console.WriteLine("c--> {0}", copylogTime);


            if ((userName == copylogUser) && (copylogUser != "Action "))
            {
                if ((computerName == copylogComputer) && (copylogTime != loginTime))
                {               
                    using (StreamWriter loginSorted = File.AppendText(@"C:\Users\jplishka\Desktop\LoginSorted.csv"))
                        {
                            loginSec = TimeSpan.Parse(loginTime).TotalSeconds;
                            logoutSec = TimeSpan.Parse(copylogTime).TotalSeconds;
                            //Console.WriteLine("secin {0} secout{1}", loginSec, logoutSec);
                            //Console.ReadLine();
                            loginSorted.WriteLine("{0},{1},{2},{3},{4},{5},{6}", copylogUser, copylogComputer, logDate, loginSec, logoutSec, loginTime, copylogTime);
                            loginSorted.Dispose();
                            break;   
                        }
                    
                }
            }


        }//loop on temp file
        copylog.Close();
    }//end findLogout
    
    
    
    public static void Main(string[] args)
    {
        string LogOn = "Log On ";
        int i;

        if (File.Exists(@"C:\Users\jplishka\Desktop\Log.csv"))
        {
            if (File.Exists(@"C:\Users\jplishka\Desktop\Temp_Log.csv"))
                File.Delete(@"C:\Users\jplishka\Desktop\Temp_Log.csv");

            if (File.Exists(@"C:\Users\jplishka\Desktop\LoginSorted.csv"))
                File.Delete(@"C:\Users\jplishka\Desktop\LoginSorted.csv");  
            
            File.Copy(@"C:\Users\jplishka\Desktop\Log.csv", @"C:\Users\jplishka\Desktop\Temp_Log.csv");
        }
        
        else
        {
            Console.WriteLine("The file \"Log.csv\" is not present");
            Console.ReadLine();
        }
        

        StreamReader log = new StreamReader(@"C:\Users\jplishka\Desktop\Log.csv");

        int maxLogins = 0;
        int currentLogins = 0;
        string strline = "";
        string logAction = null;
        string logUser = null;
        string logComputer = null;
        string logDate = null;
        string logTime = null;
        string[] value = null;

        while (!log.EndOfStream)
        {
            //Console.ReadLine();
            strline = log.ReadLine();
            value = strline.Split(',');            

            logAction = value[0].ToString();
            //Console.WriteLine(value[0].ToString().TrimStart());
            //Console.WriteLine("--> {0}", logAction);

            logUser = value[1].ToString().TrimStart();         
            //Console.WriteLine(value[1].ToString().TrimStart());
            //Console.WriteLine("--> {0}", logUser);

            logComputer = value[2].ToString().TrimStart();
            //Console.WriteLine(value[2].ToString().TrimStart());
            //Console.WriteLine("--> {0}", logComputer);

            logDate = value[3].ToString().TrimStart();
            //Console.WriteLine(value[3].ToString().TrimStart());
            //Console.WriteLine("--> {0}", logDate);

            logTime = value[4].ToString().TrimStart();
            //Console.WriteLine(value[4].ToString().TrimStart());
            //Console.WriteLine("--> {0}", logTime);
            
            //if user logs in, the find logout time
            if (logAction == LogOn)
            {
                LoginSort.findLogout(logUser, logComputer, logDate, logTime);
            }

         }//end of file reached, clean up time

        Console.WriteLine("finished making files, now time to figure out logins");
        Console.ReadLine();
        
        
        for (i=0;i<=1440;i++)
        {
            currentLogins = LoginSort.maxUsers(i);        

            if (currentLogins > maxLogins)
               maxLogins = currentLogins;
        }

        Console.WriteLine("Time for cleanup");
        Console.ReadLine();
        if (File.Exists(@"C:\Users\jplishka\Desktop\Temp_Log.csv"))
        {
            File.Delete(@"C:\Users\jplishka\Desktop\Temp_Log.csv");
        }
        if (File.Exists(@"C:\Users\jplishka\Desktop\LoginSorted.csv"))
        {
            File.Delete(@"C:\Users\jplishka\Desktop\LoginSorted.csv");
        }
        Console.WriteLine("and we're done!"); 
        Console.ReadLine();

    }//end main
}

You need to open streamReade before readint the stream.
But instead of opening, you can use the keyword "using", which will do all the needed code instead of you (open, close).

Put the StreamReader inthe the brackers of using, like this:

using(StreamReader sr = new StreamReader("filePath"))
{
    //you code for reading stream
}

Hope this helps,
Mitja

Thanks this is what I had tried before but it resulted in the same error.

public static int maxUsers(int logMinute)
    {
        string sortedstrline = "";
        string sortedUser = null;
        string sortedComputer = null;
        string sortedDate = null;
        string sortedInTimeSec = null;
        string sortedOutTimeSec = null;
        string sortedInTime = null;
        string sortedOutTime = null;

        string[] sortedvalue = null;

        double loginSec;
        double logoutSec;
        double minTime;
        double maxTime;
        int maxLogins = 0;

        using (StreamReader maxNum = new StreamReader(@"C:\Users\shep\Desktop\LoginSorted.csv"))
        {
                while (!maxNum.EndOfStream)
                {
                    sortedstrline = maxNum.ReadLine();
                    sortedvalue = sortedstrline.Split(',');

                    sortedUser = sortedvalue[0].ToString();
                    //Console.WriteLine("s--> {0}", sortedUser);

                    sortedComputer = sortedvalue[1].ToString().TrimStart();
                    //Console.WriteLine("s--> {0}", sortedComputer);

                    sortedDate = sortedvalue[2].ToString().TrimStart();
                    //Console.WriteLine("s--> {0}", sortedDate);

                    sortedInTimeSec = sortedvalue[3].ToString().Trim();
                    //Console.WriteLine("s--> {0}", sortedInTimeSec);
                    loginSec = Convert.ToDouble(sortedInTimeSec);

                    sortedOutTimeSec = sortedvalue[4].ToString().Trim();
                    //Console.WriteLine("s--> {0}", sortedOutTimeSec);
                    logoutSec = Convert.ToDouble(sortedOutTimeSec);

                    sortedInTime = sortedvalue[5].ToString().TrimStart();
                    //Console.WriteLine("s--> {0}", sortedInTime);

                    sortedOutTime = sortedvalue[6].ToString().TrimStart();
                    //Console.WriteLine("s--> {0}", sortedOutTime);


                    if ((logMinute > logoutSec) && (logMinute < loginSec))
                        maxLogins++;

                    maxNum.Close();
                }
            }

            return maxLogins;
        }

It seems that this results in errors:
while (!maxNum.EndOfStream)

whereas this can be used fine:
while ((sortedstrline = maxNum.ReadLine()) != null)

I am not sure the reason behind it though

I found the problem, You are closing the streamreader in each iteration. You should only close it after you are done, or if you use StreamReader implicitry (with keyword using), you better delete close() method (maxNum.Close() - delete it, you dont need it, implicit use of SR will close it automatically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.