Remove duplicate lines in a file that is being used by another thread

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Remove duplicate lines in a file that is being used by another thread



I have a file called example.txt and I want to remove the duplicate lines in this file using my C# application.


example.txt



My application is multi-threaded, and more than one thread are already accessing the file example.txt


example.txt



I've tried a lot of methods but all of them returns the error:



The file 0 is already being used by another process.





If the file is in use by another thread, there isn't much you can do. Attempting to read/write by multiple threads at the same time would cause data corruption. I'd implement a lock () mechanism.
– FrankerZ
Aug 8 at 7:30


lock ()





@FrankerZ - I've tried lock(locker) File.WriteAllLines(filepath, File.ReadLines(filepath) .Where(line => previousLines.Add(line))); but it is giving the same problem.
– MatrixCow08
Aug 8 at 7:52


lock(locker) File.WriteAllLines(filepath, File.ReadLines(filepath) .Where(line => previousLines.Add(line)));





Add a .ToArray() after the .Where().
– FrankerZ
Aug 8 at 8:03


.ToArray()


.Where()





@FrankerZ - File.ReadLines(filepath).Where().ToArray that can't work
– MatrixCow08
Aug 8 at 8:11


File.ReadLines(filepath).Where().ToArray





Ok, I got it to work but still the same results :/
– MatrixCow08
Aug 8 at 8:13




1 Answer
1



You should be able to do this by specifying file share in your file stream


FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);



https://msdn.microsoft.com/en-us/library/system.io.fileshare(v=vs.110).aspx





How can this be threads-safe?
– MatrixCow08
Aug 8 at 7:57





It allows you to open file by another process, you still need to implement locking mechanism
– sziszu
Aug 8 at 8:00





But why did you make it FileStream s2, why not New FileStream so it can be called immediately on a method?
– MatrixCow08
Aug 8 at 8:01


FileStream s2


New FileStream






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS