Solved

C# StreamReader

Posted on 2013-01-18
11
600 Views
Last Modified: 2013-01-22
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?
0
Comment
Question by:naelyan
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38793111
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
 

Author Comment

by:naelyan
ID: 38793309
That's exactly what I did and it doesn't seem to work.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38793328
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Expert Comment

by:navneethegde
ID: 38793377
Hi!

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

Thanks!
0
 
LVL 15

Expert Comment

by:navneethegde
ID: 38793387
So with  angelIII suggestions
you are adding extra dispose in your finally block

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

Open in new window



Thanks!
0
 

Author Comment

by:naelyan
ID: 38793594
Sorry  angelIII my bad I did see the using statement in your posted code.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38794494
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
 
LVL 15

Expert Comment

by:navneethegde
ID: 38794549
@Kaufmed : Isn't for umnanaged resources, dispose clears all resources and doesnt depend upon GC.
0
 
LVL 15

Expert Comment

by:navneethegde
ID: 38794561
@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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38794577
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
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38795984
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

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error in JQuery 5 52
SQL Exceptions 3 45
SqlDataBase 7 48
Graphics 2 28
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question