Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can't read from, then write to the same file

Status
Not open for further replies.

seantuk

Programmer
Mar 22, 2005
62
GB
i have a method that uses streamreader to read from a file, then closes it. it then uses streamwriter to write a new file. it is possible for the streamreader and streamwriter to want the same file, but when they do, the streamwriter can't open the file because it is still being used by another process.

what do i need to do to my streamreader to allow a streamwriter to write to that file?
 
Not sure exactly how you are doing it at the minute but you can use the following two functions (from
Code:
Public Function GetFileContents(ByVal FullPath As String, _
       Optional ByRef ErrInfo As String = "") As String

        Dim strContents As String
        Dim objReader As StreamReader
        Try

            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            objReader.Close()
            Return strContents
        Catch Ex As Exception
            ErrInfo = Ex.Message
        End Try
    End Function

    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean

        Dim Contents As String
        Dim bAns As Boolean = False
        Dim objReader As StreamWriter
        Try

       
            objReader = New StreamWriter(FullPath)
            objReader.Write(strData)
            objReader.Close()
            bAns = True
        Catch Ex As Exception
            ErrInfo = Ex.Message

        End Try
        Return bAns
    End Function

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
putting the code in two seperate functions hasn't helped, and i can't see anything else here that would. for future reference, my code is in c#. my actual code contains lots of stuff that isn't relevent to my problem, but here is a cut down version

Code:
//read the current contents
ArrayList fileContents = new ArrayList();
string path = Server.MapPath("myFile.txt");
StreamReader sr = new StreamReader(path);

while(sr.Peek() != -1) fileContents.Add(sr.ReadLine());
sr.Close();


//write the new contents
StreamWriter sw = new StreamWriter(path, false);
for(int i=0; i<fileContents.Count; i++)
{
  if(something())
  {
    sw.Write(different());
  }
  else
  {
    sw.WriteLine(fileContents[i]);
  }
}

sw.Flush();
sw.Close();
 
right, it's getting towards the end of the working week in england. i'll check this thread over the weekend, but before i go i want to make sure that everybody knows axactly what i'm after (especially since millions of americans have just got up, and are working there way towards this thread as i type)

i need to be able to read a file, then write over that file.

to me, that suggests i need a streamreader, which i then close. then i need a streamwriter pointing at the same file. unfortunatly, the streamreader still seems to be locking that file.

how can i make streamreader release the file?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top