Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
i am reading a file suppose that i am in line 9 i want return to line 1 again ?
is it good solution that i close file and open it again ??
it's text file

C#
 StreamReader Readfile = new StreamReader(@op.FileName.ToString());
 while ((Rline = Readfile.ReadLine()) != null)
                    {
                        temp = Rline;
                        temp = temp.Substring(1, 4).Replace(" ", string.Empty);
                        
                        if (Convert.ToInt32(temp) == Convert.ToInt32(textBox3.Text) && Convert.ToDateTime(Rline.Substring(6, 10)) >= InDate)
                        {
                            filewrite.WriteLine(Rline);
                        }
                    }
                progressBar1.Value = 20;
                Readfile.Close();
// here i want to read file again 
Posted
Updated 29-Nov-12 10:00am
v7
Comments
fjdiewornncalwe 29-Nov-12 15:24pm    
Not enough information. Is this a text file, a binary file? How is your reader set up? Show us a code snippet of how you are reading from your file and we can provide more information.

You probably might use the Stream.Seek[^] method (you should check if your actual stream supports it, see the "Remarks" section in the linked page.
 
Share this answer
 
Comments
farham_heidari 29-Nov-12 15:50pm    
i read the file one time i just want start again
lewax00 29-Nov-12 16:57pm    
Then seek to 0. Did you read the page he linked to at all?
Sergey Alexandrovich Kryukov 29-Nov-12 18:00pm    
Valid, my 5. But makes little to know sense in many cases, most likely, including OP's
Also, it should support Seek as it is a StreamReader over a file stream, as you can see.

Please see my answer -- also a couple of problems found...
--SA
In most cases, this is bad practice. You are only reading, so why would you ever come back? Remember result of the first line reading and go forward. Actually, if the file is not too big, you can gain a lot if you read everything at once and then only manipulate the string, using System.IO.StreamReader.ReadToEnd:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend.aspx[^].

Now, a couple of notes:

Your FileName.ToString tells me a lot. What, don't you see that the file name is already a string?

Also, this is not a good pattern of using the types like StreamReader. It implements System.IDisposable, so you can use it via the using statement (not to be confused with using declaration):

C#
using (StreamReader reader = new StreamReader(@op.FileName.ToString())) {
    // use reader here
}   // here, reader.Dispose is automatically called; it will be closed,
    // the stream buffer won't be lost (especially important in case of StreamWriter)

Please see:
http://msdn.microsoft.com/en-us/library/system.idisposable.aspx[^],
http://msdn.microsoft.com/en-us/library/yh598w02%28v=vs.80%29.aspx[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900