Problem opening and reading file [modified]
-
I am trying to read text from a file, however, I keep getting "The process cannot access the file ... because it is being used by another process." The issue is, I know that another process has the file open, but I need to be able to read the file without closing that process. The weird thing is, I can open and edit the text file with notepad, but all the methods I have tried thus far in C# keep throwing the access error. Here is one of the ways I am trying to open the file:
FileStream file; TextReader reader; try { file = new FileStream("C:\\file.log", FileMode.Open, FileAccess.Read, FileShare.Read); reader = new StreamReader(file); string test = reader.ReadLine(); MessageBox.Show(test); } catch(Exception ex) { MessageBox.Show(ex.ToString()); }
-- modified at 11:55 Friday 16th June, 2006 -
I am trying to read text from a file, however, I keep getting "The process cannot access the file ... because it is being used by another process." The issue is, I know that another process has the file open, but I need to be able to read the file without closing that process. The weird thing is, I can open and edit the text file with notepad, but all the methods I have tried thus far in C# keep throwing the access error. Here is one of the ways I am trying to open the file:
FileStream file; TextReader reader; try { file = new FileStream("C:\\file.log", FileMode.Open, FileAccess.Read, FileShare.Read); reader = new StreamReader(file); string test = reader.ReadLine(); MessageBox.Show(test); } catch(Exception ex) { MessageBox.Show(ex.ToString()); }
-- modified at 11:55 Friday 16th June, 2006 -
This code seems me ok, I think there could be some issues related to file/system security. Riz
-
I am trying to read text from a file, however, I keep getting "The process cannot access the file ... because it is being used by another process." The issue is, I know that another process has the file open, but I need to be able to read the file without closing that process. The weird thing is, I can open and edit the text file with notepad, but all the methods I have tried thus far in C# keep throwing the access error. Here is one of the ways I am trying to open the file:
FileStream file; TextReader reader; try { file = new FileStream("C:\\file.log", FileMode.Open, FileAccess.Read, FileShare.Read); reader = new StreamReader(file); string test = reader.ReadLine(); MessageBox.Show(test); } catch(Exception ex) { MessageBox.Show(ex.ToString()); }
-- modified at 11:55 Friday 16th June, 2006You're only reading one line, is this code in a loop? Can't help much without seeing the context in which that code executes. Of course there is the possibility that another process is using the file. It's a log file, is there something that's logging to it? Try something like...
try {
string text = string.Empty;
using (StreamReader reader = new StreamReader(@"C:\file.log")) {
text = reader.ReadToEnd(); // or ReadLine()
reader.Close();
}
} catch {}Visit BoneSoft.com