C# StreamReader

Hi Guys,

I'm using a StreamReader object to read the contents of a text file one line at time. Somthing along the lines of the following:

System.IO.StreamReader file = new System.IO.StreamReader("c:\test.txt");

try
{
while((line = file.ReadLine()) != null)
{
//Do Stuff
}
}catch(exception ex)
//Error Handler
{
finally
{
file.close();
}

The problem I have is that when I open the file manually (with the app still open) I can't make changes. Doesn't the close() method release the resource?
naelyanAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
you may try this syntax:
using ( System.IO.StreamReader file = new System.IO.StreamReader("c:\test.txt"))
{
try
{
while((line = file.ReadLine()) != null)
{
//Do Stuff
}
}catch(exception ex)
//Error Handler
{
finally
{
file.close();
}
} 

Open in new window

0
 
naelyanAuthor Commented:
That's exactly what I did and it doesn't seem to work.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
actually, the "using" syntax will add a implicit "file.dispose()", and should work.
so, my code suggestion is NOT the same as your code.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
NavneetSoftware EngineerCommented:
Hi!

Close vs Dispose
http://forums.asp.net/t/1744269.aspx/1

Thanks!
0
 
NavneetSoftware EngineerCommented:
So with  angelIII suggestions
you are adding extra dispose in your finally block

finally
{
file.close();
file.dispose();
}

Open in new window



Thanks!
0
 
naelyanAuthor Commented:
Sorry  angelIII my bad I did see the using statement in your posted code.
0
 
käµfm³d 👽Commented:
Actually, for streams (and StreamReaders), the Close method calls Dispose, so functionally there is no difference in either approach.

Doesn't the close() method release the resource?
It does, but you have to understand that resources aren't necessarily released at the instant you call Dispose. It is up to the GC as to when it releases resources. You can force a GC run by calling GC.Collect();, but this is generally bad for performance.
0
 
NavneetSoftware EngineerCommented:
@Kaufmed : Isn't for umnanaged resources, dispose clears all resources and doesnt depend upon GC.
0
 
NavneetSoftware EngineerCommented:
@kaufmed : I mean to say releasing of resources are in dispose pattern imlpementation by assembly author in case unmanaged code, generally in case of File operation.
0
 
käµfm³d 👽Commented:
Unfortunately, I'm not deeply versed in the CLR and the GC. My understanding is that it should be, but I have had really tight loops that dealt with files which still generated locking errors. It could have been my naiveté with how I wrote the code, though. To the best of my knowledge (and my practical experience), what is written above (or the alternative using approach) should work.
0
 
Naman GoelSoftware engineer 1Commented:
You have to specify FileShare Mode for achieving this:

using System;
using System.IO;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = new StreamReader(new FileStream(@"c:\new1.txt", FileMode.Open,  FileAccess.Read,  FileShare.ReadWrite)))
            {
                string line = String.Empty;
                while ((line= sr.ReadLine())!=null)
                {
                    sb.Append(line);
                }
            }
        }
    }
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.