FileSystemWatcher Distorts File Name

I use FileSystemWatcher to detect .docx files. Files are detected upon opening, however the file name is always "corrupt."

3 examples:

  • If my file name is: 2711111.docx, the file name obtained in FileSystemWatcher.Changed is: $ 711111.docx.

  • For the file: * 2711111_1.docx * I get the file name: * ~ $ 11111_1.docx * I can't figure out what my file name will be, so I'm looking for a general solution.

  • For a file contains / starts with a letter, this does not happen.

Here is my code

 MyPath = String.Format(@"C:\Users\{0}\NRPortbl\ACTIVE\{1}\"", Public.UserName, Public.UserName); FileSystemWatcher watcher = new FileSystemWatcher(); if (!System.IO.Directory.Exists(MyPath)) { Public.Logger.Error( String.Format ("Path of folders {0} not found", MyPath)); System.Windows.Forms.MessageBox.Show( String.Format( "There was a problem loading {0} " + "NRPortbl libraray, contact administrator", Public.UserName)); return; } watcher.Path = MyPath; watcher.Filter = "*.Docx"; watcher.IncludeSubdirectories = false; watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnDeleted); watcher.EnableRaisingEvents = true; ... public void OnChanged(object source, FileSystemEventArgs e) {...} 

Help would be greatly appreciated :) Thank you!

+4
source share
3 answers

This is a design for Microsoft Word. It creates a hidden file when the user opens the document. This file records the username so that when someone else tries to open the same document, they will receive a decent message that tells them that the user is currently open the document for editing.

The file name of this hidden file is the original file name in which the first two characters are replaced with ~$

Usually you do not see this file when browsing a directory using Explorer, because it is included in the FileAttributes.Hidden attribute. Of course, you should also ignore these files, use the FileInfo.Attributes property to filter them.

+5
source

Subscribe to several other events, such as renaming and displaying their file names.

I suspect that you are seeing temporary file names that are renamed to the actual file name by renaming.

+4
source

Unconfirmed code, but I remember how I did the trick like before.

First of all, when you open a file or save a file, the OnChanged event will (hopefully) fire several times. This way you can see that at some point you get the correct file name. To see this, you can use the file existence function and some other methods. About one way or another:

 public void OnChanged(object source, FileSystemEventArgs e) { if (e.FullPath.Contains("~$")) //to avoid the corruption you are talking about. return; //or better handling - trivial if (!File.Exists(e.FullPath)) //to avoid some temp files that need not be visible return; //but happens with doc files. //got the file e.FullPath } 

If you have not received the required file, you can subscribe to another event - the rename event.

+1
source

All Articles