c# Reading file occurs The process cannot access the file 'Path' because it is being used by another process.

                using(var fs = new System.IO.FileStream(@"Path", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                using (var sr = new System.IO.StreamReader(fs))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }

If you just need whole text not lines, then use this code for better performance
                            using (var fs = new System.IO.FileStream(res.path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
                            using (var sr = new System.IO.StreamReader(fs))
                            {
                                return sr.ReadToEnd();
                            }

No comments:

Post a Comment