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

VulpesRank #55 · 29.2K reputationPosted Jun 10, 2011, 8:38 AM EDT
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);
Marius17 reputationPosted Jun 10, 2011, 8:52 AM EDT
The FileShare thing doesn't change anything :(
VulpesRank #55 · 29.2K reputationPosted Jun 10, 2011, 8:56 AM EDT
Marius17 reputationPosted Jun 10, 2011, 9:07 AM EDT
Sam HobbsRank #108 · 20.5K reputationPosted Jun 10, 2011, 9:17 AM EDT
Marius17 reputationPosted Jun 10, 2011, 9:27 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jun 10, 2011, 9:54 AM EDT
Is it another process running on the same machine?
Marius17 reputationPosted Jun 10, 2011, 10:48 AM EDT
VulpesRank #55 · 29.2K reputationPosted Jun 10, 2011, 10:53 AM EDT
Sam HobbsRank #108 · 20.5K reputationPosted Jun 10, 2011, 12:26 PM EDT
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.
Marius17 reputationPosted Jun 11, 2011, 2:03 AM EDT
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.
VulpesRank #55 · 29.2K reputationPosted Jun 11, 2011, 4:03 AM EDT
Marius17 reputationPosted Jun 11, 2011, 6:11 AM EDT
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?
Sam HobbsRank #108 · 20.5K reputationPosted Jun 11, 2011, 7:16 AM EDT
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();
}
VulpesRank #55 · 29.2K reputationPosted Jun 11, 2011, 9:23 AM EDT
I can't find any option to open the file with different file share settings, in any case.