Issue when putting a FileSystemWatcher if explorer.exe is open - c#

For a project, I needed some FileSystemWatcher on directories. But I got a weird issue : after having launch the app, I can choose the directories where I want to put FileSystemWatcher; if the explorer.exe isn't open when I start to raise events in a directory, I can open it to do stuff in the watched directory. In this case, handlers are fired, and all works perfectly.
Let's imagine I let the explorer.exe open, and I put a FileSystemWatcher in another directory via my app. From this point, I can't modify anything in this second directory : when I modify / rename a file (there's no error during creation of files. EDIT : and I can modify the new file, change his name, etc...), Windows (note this is not the application, but the OS) throws me an error which means "the process can't access this file because of another process accessing it right now".
Does someone have any idea about this, please ?
EDIT : okay, I'm dumb. I just forgot to close a stream...

Related

FileSystemWatcher locks folder

I have a FileSystemWatcher listening on a folder. When I try renaming this folder (in Windows Explorer) I get a
The action can't be completed because the folder or a file in it is
open in another program
error.
Commenting out the FileSystemWatcher calls fixes this.
I've tried reproducing the error with a simple program, but haven't been successful so I'm not sure what's going on here. Has anyone encountered anything like this with a FileSystemWatcher? If so - what might be causing it?
More information:
The following seems to be enough to cause the error:
FileSystemWatcher fsw = new FileSystemWatcher(path);
fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
fsw.EnableRaisingEvents = true;
I can rename files in that folder. And sibling files. I even tried putting a return; as the first line in fsw_Deleted. Still didn't work. As didn't closing Visual Studio, and deleting obj and bin, and running again. And I even tried running the exe file not within VS. Still - no lock if it's not running, Locked if it is.
I've discovered what caused this. There was a second FileSystemWatcher - on a sub directory of the first - which didn't allow renaming the first.
(I'm still surprised, though. A FileSystemWatcher should be "invisible".)
Its going to do that, since your process is looking for files in that folder. What you may want to do is add a handler for fsw.Rename. Then inside that process you should do a RemoveHandler on looking for new files, and then add it back in with the new name of the folder.

Use file system watcher to monitor files being copied?

i am doing an application that uses file system watcher and the main aim is to monitor files that are being copied , so i looked at the file system watcher method i found 4 events i can sue , they are change , delete , rename and create , i didn't find the copy event , umm and i'd like to watch specific files and when a user tries to copy the files i prevent him from doing so using the file system watcher method , so in file system watcher is there any method for copying files monitoring that i can use ? , sorry my question may look stupid but this is the first time i use file system watcher and i read about it a lot and almost all people agree about the 4 events that can be used in that class .
There is no operation called copying a file.
Rather, copying is a combination of reading one file and writing another one.
You cannot reliably do this.
You cannot do this with FileSystemWatcher
May be monitor Clipboard and if you find list of files, check location of it
I think you can at least have a notification of who copied the file by using WMI notification. Another option is restricting user access to the folders.
Try this link.
http://technet.microsoft.com/en-us/library/ee176985.aspx#EHAA
Why don't you check that the executable is launched from the usb key at the beginning of your software ? this way the user who copied the file won't be able to launch it even if he copied the exe.

Intermittent "File in Use" Error

I have some code that I wrote to basically clear out the directory every time the program runs through this point. I didn't want to bother enumerating files. If this is a bad way to do this, please tell me.
My main question, however, is about how to deal with the following: one of the files in the folder appears to be in use when it is most certainly not. The program runs on a ButtonClick event, and it exploded the first four or five times, but it worked after I confirmed that nobody was using the file on the server. There is only one person besides myself that would have been using it, and he confirmed that there was nothing running on his side that would be touching the file. Any ideas for what would cause this error/how to avoid it/how to handle it?
I am also having trouble reproducing the error...
string directory = #"\\server\directory\folder\";
DirectoryInfo di = new DirectoryInfo(directory);
if (di.Exists)
di.Delete(true);
Directory.CreateDirectory(directory);
If you are using Windows XP, this may help : http://msdn.microsoft.com/en-us/library/dd997370.aspx#remove_open_handles
Just an extract from the top of this page :
"If you are running Windows XP or earlier, a delete operation on a file or directory that follows an enumeration could fail if there is an open handle that remains on one of the enumerated directories or files."
You may also use a software like Unlocker to identify the process locking your file.
If the file is in use, then someone is most certainly using it. :)
If you can access the server the files reside on, you can use a tool such as Process Explorer to find out which process has opened the file.

Is there a way in c# to know when the user has finished editing a file with an application?

This is what I'm trying to do :
Download a file (txt, doc, xls, whatever) from a server
Open the file with the appropriate application using System.Diagnostics.Process.Start(path to file)
Monitor for file changes using a FileSystemWatcher.
Each time the file is changed, upload the file back to the server
Continue monitoring until the user has finished editing the file
Delete the local copy of the file
Exit the application
I'm stuck at step 5. How can I know whether a user has finished working on a file ?
I cannot rely on the file being locked (notepad doesn't lock txt files for example).
I cannot rely on a process having exited or not (an example is Notepad++ for txt files : the file could be open in a tab. When you close the tab, you've finished editing the file, but the process is still running)
Any idea/points on how to do that in C# ?
You've excluded the two ways you could go about detecting the file being in use: file locking, and the process you start exiting.
The only alternative I an think of is to display a dialog to ask the user when they've finished editing.
Edit: For what it's worth - FileZilla has this type of behaviour. You can choose to edit a file on the remote server, it downloads the file, launches the default editor, and (in the background) shows a "If you've finished editing - Click OK" button.
This gives me the opportunity to cancel an edit, if I've mucked up the file and saved it.
This is really hard to do - we've tried various things but never found anything that was foolproof. If you know the program you have launched then, in theory, you can find the file handles it uses and see when it stops using the one you're interested in.....but if you rely on Windows to resolve the default application to launch even this becomes tricky.
We copy editable files into a temp folder named with the date and rely on users uploading them back when they have finished their edit session. We then clean up previous days folders on application startup.
You could check the date of last change of the file. This date gets set when you save changes to the file. Mind though that this field is not very reliable since one can set it to any value (with appropriate tools).

events in a file system watcher

when i use a file system watcher changed event for a notepad it occurs once,but the event occurs twice for a word pad,please give me the reason.i launch using explorer
A bit Briefly,
I have a file named "xxx.log" it contains some strings,i wrote a filesystemwatcher which will watch this file for size(notifyfilter - size) changed.whenever
i opened the file with notepad the changedevent occurs once,whenevr i do it with wordpad
the event occurs twice what is the reason. i open files using explorer.exe.
how to handle this problem.i need the event once only,is there any way
I think that notepad has no lock/backup file handling. Thus it writes only once to the file - Word and Wordpad work with some temporary files for backup and locking purposes and for that reason they might write twice.
The best way to understand this is to use process monitor from sysinternals. This will show you exactly what is going on.

Categories