So, my situation is something like this:
What I want to do is to detect when the contents of a file change, and then read from that file (and use the data for whatever). The trouble is, when I try to do that, I get a "File is in use" exception.
Some code:
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Path = pathToFile;
watcher.Filter = fileName;
//then the event:
watcher.Changed = new FileSystemEventHandler(watcher_Changed);
watcher.EnableRaisingEvents = true;
//and the handler
void watcher_Changed(object sender, FileSystemEventArgs e)
{
FileStream fs = new FileStream(pathToFile + fileName, FileMode.Open, FileAccess.Read);
//read from file, etc etc
}
So, this basically says that the file cannot be accessed because it's open by another process. I assume the FileStream locks the file or something like that?
It works when not trying to read from the file...
Anyway I can fix this?
Thanks in advance.
Loading
VulpesPosted Jun 11, 2011, 1:23 PM
I can't find any option to open the file with different file share settings, in any case.
Sam HobbsPosted Jun 11, 2011, 11:16 AM
The following is for a simple console application. I can execute the program and while it is executing I can have a file open in VS and edit the file and save the changes and this program then reads it.
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Path = pathToFile;
watcher.Filter = "";
watcher.EnableRaisingEvents = true;
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
Console.ReadKey();
}
static void watcher_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File has changed: {0} {1}", e.Name, e.FullPath);
using (StreamReader sr = new StreamReader(e.FullPath))
{
while (!sr.EndOfStream)
Console.WriteLine(sr.ReadLine());
}
Console.WriteLine();
}
MariusPosted Jun 11, 2011, 10:11 AM
For instance, is there some setting in VS that allows me to access the file from within the application, while it's also open in the VS editor?
VulpesPosted Jun 11, 2011, 8:03 AM
MariusPosted Jun 11, 2011, 6:03 AM
To clarify further, this is a file that contains GLSL code (shading language, runs on the GPU, etc). What I want is to have it automatically commit the changes that I make to the file in the resulting rendered image. I don't want to have to close the application and relaunch it, as there is a lot of pre-processing being done and it takes quite a while. Right now I have a button that I click on after I've made modifications to the shader file, but it would still be a lot faster if the changes would take place when I modify the file and hit Ctrl+S.
Sam HobbsPosted Jun 10, 2011, 4:26 PM
It is easy enough to just exit from VS and try the application.
Earlier when I said to Dispose the FileStream object I should say to Dispose of anything in your application that used the file.
Try the Handle utility. I am not sure whether it will tell you what you need to know. Note that it is a command-line tool and if you don't specify any options it will produce many lines of output. I think I would begin using it by redirecting the output to a file.
VulpesPosted Jun 10, 2011, 2:53 PM
MariusPosted Jun 10, 2011, 2:48 PM
VulpesPosted Jun 10, 2011, 1:54 PM
Is it another process running on the same machine?
MariusPosted Jun 10, 2011, 1:27 PM
Sam HobbsPosted Jun 10, 2011, 1:17 PM
MariusPosted Jun 10, 2011, 1:07 PM
VulpesPosted Jun 10, 2011, 12:56 PM
MariusPosted Jun 10, 2011, 12:52 PM
The FileShare thing doesn't change anything :(
VulpesPosted Jun 10, 2011, 12:38 PM
I'd try using one of the overloads of the FileStream constructor which takes a FileShare argument:
FileStream fs = new FileStream(pathToFile + fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);