Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
lock(_syncobj)
{
    using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
    {
    // coding to read data
    }
    
    FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
    using (TextWriter tw = new StreamWriter(fileStream))
    {
    //coding to write data
    }
}

I have used FileShare.Read for reading and FileShare.None for writing and used lock also but also some IO Exception is occured stating that the file is used by another process.
Posted
Updated 16-Apr-12 0:40am
v2
Comments
VJ Reddy 16-Apr-12 6:40am    
Edit: pre tag for C# code added.

do one thing

first read the file and place the whole data in some variable then close the opened stream
and now open it in write mode and write the data to the file.
 
Share this answer
 
On the FileStream that only READS the file, you need to set it as
C#
FileShare.ReadWrite
 
Share this answer
 
v2

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