Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I am trying to Read and Write the same file at a time but it is showing error like this

The process cannot access the file 'D:\sample2.txt' because it is being used by another process.
C#
using (StreamReader sr = new StreamReader(@"D:\sample1.txt"))
            {
                using (StreamReader srn = new StreamReader(@"D:\sample2.txt"))
                {
                    using (StreamWriter sw = new StreamWriter(@"D:\sample2.txt"))
                    {
                        while (!sr.EndOfStream)
                        {
                            string actualURL = sr.ReadLine();
                            while (!srn.EndOfStream)
                            {
                                string uniqueURL = srn.ReadLine();
                                if (!actualURL.Equals(uniqueURL))
                                {
                                    sw.WriteLine(uniqueURL);
                                }
                            }
                        }
                    }
                }
            }


How to solve this issue???

My Requirement:
In the Sample1 text file some repeated URLs present, i want to take out that repeated URLs.

Sample2 text file does not contain any thing

My approach:
I am checking that this string is present in the Sample2 text file or not
if not present than write in the sample2 text file else not.

But this is showing this type of error

Please help how to solve the problem and please identity if i have done any mistake

Thanks in advance
Posted

You can't open a reader and a writer on the same file!
You can do it by opening a file for read-write access, becasue that can be specified as a nonexclusive lock., but trying to do exactly what you are doing will probably cause big problems: You need to output to a third file, then rename it at the end after deleting the second.
 
Share this answer
 
Comments
Mac12334 31-Jul-12 6:48am    
Thanks for reply
OriginalGriff 31-Jul-12 6:59am    
You're welcome
C#
using (StreamReader sr = new StreamReader(@"D:\sample1.txt"))
            {
                while (!sr.EndOfStream)
                {
                    FileStream fileStream = new FileStream(@"D:\sample2.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
                    using (StreamReader srn = new StreamReader(fileStream))
                    {
                        using (TextWriter sw = new StreamWriter(fileStream))
                        {
                            string actualURL = sr.ReadLine();

                            while (!srn.EndOfStream)
                            {
                                string uniqueURL = srn.ReadLine();
                                if (string.Compare(actualURL,uniqueURL,false) == 0)
                                {
                                    sw.WriteLine(actualURL);
                                }
                            }
                        }
                    }
                }
            }


Try the above code..

OriginalGriff is also write
 
Share this answer
 
Comments
Mac12334 31-Jul-12 6:52am    
Thanks for reply
Mac12334 31-Jul-12 6:54am    
Its full fill my requirement and working fine

Thanks Bhagirathimfs
 
Share this answer
 
Comments
Mac12334 31-Jul-12 6:48am    
Thanks For reply
I think we cannot read and write a page at a time. In this you reads and writes data to sample2.text at a time .This will create error.
 
Share this answer
 
v2
Comments
Mac12334 31-Jul-12 6:05am    
Thanks for reply

Can you please a way to solve this problem??
please
Mac12334 31-Jul-12 6:48am    
Thanks for reply
Try this - Insted of opening for write use arraylist to add data. After complete reading done write in file.
ArrayList ar = new ArrayList();

        using (StreamReader sr = new StreamReader(@"D:\sample1.txt"))
        {
            using (StreamReader srn = new StreamReader(@"D:\sample2.txt"))
            {             
                    while (!sr.EndOfStream)
                    {
                        string actualURL = sr.ReadLine();
                        while (!srn.EndOfStream)
                        {
                            string uniqueURL = srn.ReadLine();
                            if (!actualURL.Equals(uniqueURL))
                            {
                                ar.Add(uniqueURL);
                            }
                        }
                    }
              
            }
        }
        using (StreamWriter sw = new StreamWriter(@"D:\sample2.txt"))
        {
            foreach (string temp in ar)
            {
                sw.WriteLine(temp);
            }
        }
 
Share this answer
 
Comments
Mac12334 31-Jul-12 6:48am    
Thanks for reply

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