I'm looking for a clever way to implement a while loop in C# that loops continously until FileInfo's LastAccessTime is changed.
I'm using a FileSystemWatcher to raise a change event where I do some work on the file, but this one fires as soon as I open the file.
Any ideas?
Have a look at the NotifyFilter property on the FileSystemWatcher. FileSystemWatcher does support looking at last access time.
FileSystemWatcher seems like the right way to do this. To only get notifications for LastWrite, set the NotifyFilter property to reflect that:
watcher.NotifyFilter = NotifyFilters.LastWrite;
Related
I'm trying to fiddle with an idea, and one big concept is dropping programs, bookmarks, etc. I have what I believe to be the gist of it but I'm not receiving any information
private void Border_Drop(object sender, DragEventArgs e)
{
FileInfo[] files = (FileInfo[])e.Data.GetData(DataFormats.FileDrop);
foreach (FileInfo file in files)
{
Writer.Text += file.FullName;
}
}
Though my textblock is never populated? I'd like to get all the properties of File such as FullName, Name, Extension, DirectoryName, etc.
Any pointers on where to go from here? I've been trying to read the MSDN of the event but nothing is coming up, and I've searched stackoverflow and nothing is working for my case scenario.
One possible reason is that you have attached the event handler to the wrong control. Try attaching the event handler to the Form, or if that does not work, the textblock.
I would also highly recommend doing some debugging. Place a breakpoint in the event handler to see if it is called, and if so, what happens in it.
WPF has two different routed event handling mechanisms: bubbling and tunneling. Bubbling is the "normal" way to do it, and tunneling is when you see all those PreviewThis and PreviewThat in the event name. It's possible, that another control has aready marked the event as handled, and so you don't see it in your event handler.
If you need documentation for WPF drag and drop, read this
If you need some really custom, really low level stuff on this subject, you can also read up on Object Linking and Embedding This is probably how drag-and-drop is implemented in Word so that you can drag images and Excel tables into Word documents and they will display natively. But to use this you will probably need to use some Win32 API calls which is a bit of a bummer.
I have to add footnotes to my plugin and need a monitor to know when Footnotes added to document, Is there an event like Footnote_Add, Footnote_Edit and Footnote_Delete, such event in contentcontrols ContentControlAfterAdd for Footnotes ?
I don't believe so, there are no events for the Footnotes object.
Instead try WindowSelectionChange of the Word Application object. But ensure to use performances as less as possible since that event is raised regularly.
Let's say,
I have an instance of FileInfo,
I want my program to check for file changes and
the FileInfo class doesn't provide events for that task.
Is there a better way than using a timer to check the FileInfo's properties and throw an event, if those properties changed?
I want to check a file for changes without opening it.
FileSystemWatcher.Changed event is what you are looking for.
i have a question about the moving file event on filesystemwatcher class , i`d like to stop the moving of file or edit it when the moving file event arises for a certain file , is that possible to handle inside the moving event ?
No, there is no way to stop someone moving or renaming a file using the FileSystemWatcher class.
If you look, none of the event arguments passed by the events on the FileSystemWatcher class have a Cancel property. Also, the fact that the class is simply called a Watcher is a bit of a clue.
You might consider using Access Control Lists to make sure someone cannot delete a file (since a move is really just a copy/delete). Or perhaps you could try opening a FileStream on the file so that you have it locked.
You cannot directly "cancel" the operation by means of the event handler. You would have to provide a compensating operation to programatically "undo" any changes you want undone.
You only receive the events after the fact has happened. It's a mere notification, not an event you'd have to approve. This can also be guessed from the missing Cancel or Handled property in the FileSystemEventArgs (as opposed to, for example, the KeyEventArgs) class.
You can detect a move and try to move the file back, based on the OldFullPath property of the RenamedEventArgs you receive.
This might however be confusing to your users or to other software. And try not to end up in an infinite loop, where you move the file back and forth every time you receive the event.
Suppose i have a textfile which continuous to be updated with text, how can i display the contents in a textbox in windows form (in real-time)?
for example. this is the contents of log.txt:
connected, bla bla bla
disconnected, bla bla bla
PS: i want it to be displayed in textBox of Form1 (real time also) so everytime the text file has new text, the textbox displays it. Any ideas, pls help. thanks. also can u provide a sample working code. thanks
Use a FileSystemWatcher
Use the example on MSDN (see the link above) you can add a event handler to Changed Event
and update your textbox from there.
Just call Application.DoEvents(); once you've updated your text. But note that updating too often might cause flicker as well as slow down your overall processing. Also note that this is error prone if any drawing/update code causes updates/draws again (infinite recursion).
Edit: Read half the question ...
Add a FileSystemWatcher to watch for changes to the log file. However this is a very ineffective approach and depending on the settings used to write the log file you might be missing access rights and/or the file might update only when the application closes. If both processes are your own code and you're able to modify them, you should think about other possibilities (e.g. a simple "server" you can connect to using some telnet (or custom) client).
Insert a RichTextBox and a FilesystemWatcher, and a textbox or something else to store the path and filename of the text file you want to show. Now, go to the FilesystemWatcher_Changed (or whatever it's called) event. There you put an if that checks if the changed file is the one you want to be shown, and then RichTextBox.LoadFile(file).
What this is going to do is monitor the fileystsem (hard disk or any folder you specify), and every time a file has cha nged, it's going to check if it's the file that we're looking at. If it is, it's going to reload the file into the RichTextBox.
Don't forget to set the RaiseEvent and Path properties of the FilesystemWatcher.
Example:
string blaFile = "C:\TextFile.txt"; // The text file that we want to read.
private void FilesystemWatcher_Changed(....).... // The FilesystemWatcher_Changed event notifies us when a file on the monitored path (FilesystemWatcher.Path) has changed. When a file has changed the code within this event gets executed.
{
if (e.File == blaFile) // We check if the file that has chenged, is the one we want.
{
RichTextBox.LoadFile(blaFile); // It is! We load it into the RichtextBox again so that it's "up-to-date"!
}
}
I'm not completely sure about the e.File as I haven't used C# in a while now, but it's something like that.
It is not suggested to use text file, since there are two processes to access the file and you require real-time update.
Try to use NamePipeServerStream and NamePipeClientStream for two processes memory sharing.