Hey i´m having trouble with some programming in C#. I want to read a file every time it changes.
try
{
using (FileStream file = new FileStream(@"C:\myFliles\test.txt", FileMode.Open, FileAccess.Read))
{
StreamReader sr = new StreamReader(file);
string s = "";
while ((s = sr.ReadLine()) != null)
{
if (sr.EndOfStream)
{
sr.Dispose();
file.Dispose();
sr.Close();
file.Close();
MessageBox.Show(s);
break;
}
}
}
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}¨
The this code works fine the first time the file changes, but the second time i change the file an exception is raised that says that the file already are in use by another process. How can I solve this problem?
// Linus
Loading
Linus MartinssonPosted Apr 29, 2010, 1:10 PM
Now it works fine.
I u you one :)
Jaish MathewsPosted Apr 28, 2010, 1:24 AM
Good new is that I could able to reproduce your exception. As I expected, you may trying scenario which is not real time. Below is the flow I tried and got exception.
Resolution
In real time it may not going happen. But we need to consider the whole scenarios. So I fixed this by running the "onchnage" in a separate Thread so that no conflict with System operation. So modify the "OnChanged" like below. You won't write any direct code. But will call a new method in a separate Thread.
using (StreamReader sr = new StreamReader(@"C:\Larm\test.txt"))
{
MessageBox.Show(sr.ReadToEnd());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Linus MartinssonPosted Apr 27, 2010, 3:27 PM
by the way now it´s the "StreamReader sr = new StreamReader(@"C:\Larm\test.txt")" that throws the exception
Jaish MathewsPosted Apr 27, 2010, 2:22 PM
Use this simplified OnChange i.e. only one Reader object is enough and no need of close() on top of "using" blocks .
More over I uploaded my working project. Just compare the whole program flow as this project is running without any problem.
private static void OnChanged(object source, FileSystemEventArgs e)
{
try
{
using (StreamReader sr = new StreamReader(@"C:\Larm\test.txt"))
{
MessageBox.Show(sr.ReadToEnd());
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Linus MartinssonPosted Apr 27, 2010, 1:54 PM
private void startFileWatcher()
{
watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.Size;
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
try
{
using (FileStream file = new FileStream(@"C:\Larm\test.txt", FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(file))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
if (sr.EndOfStream)
{
sr.Close();
file.Close();
MessageBox.Show(s);
break;
}
}
}
}
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
Okay here is my modified code :). It still wont work and its the line "FileStream file = new FileStream(@"C:\Larm\test.txt", FileMode.Open, FileAccess.Read))" that thows the exception. Translated from swedish the error message is "The process can not reach the file C:\Larm\test.txt" beacuse it is used by another process. should not the line file.Close(); realise the file C:\Larm\test.txt?
Jaish MathewsPosted Apr 27, 2010, 12:07 PM
2 things.
Linus MartinssonPosted Apr 27, 2010, 10:30 AM
private void startFileWatcher()
{
watcher = new FileSystemWatcher();
watcher.Path = @"C:\Larm\test.txt";
watcher.NotifyFilter = NotifyFilters.Size;
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
try
{
using (FileStream file = new FileStream(@"C:\Larm\test.txt", FileMode.Open, FileAccess.Read))
{
StreamReader sr = new StreamReader(file);
string s = "";
while ((s = sr.ReadLine()) != null)
{
if (sr.EndOfStream)
{
sr.Close();
file.Close();
MessageBox.Show(s);
break;
}
}
}
}
here is the all av the code. When i wite in the file the first time it works perfekt, but the secondtime that the file is already in use by another process. If i use a button to check the file instead of using the OnChange event it works again.
The code is supposed to check a file for incoming messages and then read the message and send it to another client. Can I solve this problem by using som other solution instead of checking the file for changes? Any ideas ?
Vladimir NaniPosted Apr 27, 2010, 1:20 AM
If you are running your application than make some changes in your code and try to build it you can get:
Error 1 Unable to copy file "obj\x86\Debug\Corner.exe" to "bin\Debug\Corner.exe". The process cannot access the file 'bin\Debug\Corner.exe' because it is being used by another process.
By the way, you don`t need to call Dispose method if you are calling Close because Close implicitly invokes Dispose.