Send email after folder changes - c#

I'm looking for some advice and help. On Windows Server in ASP.NET C#:
I have 10 folders (with subfolders and files) associated in a DB to 10 users (columns: foldername, user).
I modify the content of the folders, then I press a button and to users with folder changed arrive an alert mail.
I thought to use FileSystemWatcher, but this fires automatically for every change (I'm wrong?), and I don't need it.
I think it is more simple to create an "imprint" of every folder and store it in the DB for every user (columns: foldername, user, imprint, imprint_date). When I press the button, if the new imprint is different from previous imprint, the system will send an email to the user (and then store the new imprint in the DB).
What do you think about this solution? Can someone can suggest to me how I can create an imprint?

You could traverse the directories and files saving into a file the name + last modified date, and save that as a string.
Whenever you want to check, compare the strings.
You could also simply hash them and see if the hash has changed (which is not 100% accurate since in some end cases you might miss a notification), but should be more than good enough for what you seem to need ...
Edit:
As for your comment, no implementation I'm aware of, but it shouldn't be hard to write.
I gave your problem a little though ... You could actually use the FileSystemWatcher, but with a twist ... Have a class create 10 watchers, and add their event handlers so they monitor each folder.
When a change fires, in that class, set a bool flag for that user to true (call it ChangesMade for example), and unregister the FileSystemWatcher. You get no more events fired when more changes are done.
When you hit the send emails buttons, just send emails to whomever have their ChangesMade property set to true and register those FileSystemWatcher again for those folders.

Related

Microsoft Sync Framework, Detect changes first and then sync

I have two folders that have some files inside them, I want to use Microsoft Sync Framework in a way that first it detects changes in a folder, if there is any, then carry out the Sync operation with the other folder.
The idea behind detecting the change on the folder is that I could query on that change and can do some operations first, before sync.
Any idea is also welcome to use the MSF with other techniques to achieve the same.
I have tried the sample code example given in this link https://msdn.microsoft.com/en-us/library/mt763483.aspx
But it first sync the folders and then fire some events. I try to fire the events first but it doesnt work.
I am a beginner in all of this so any help in this regard is higly helpful.
There is an event "ApplyingChange" which is triggered whenever there is a change in the folder. In this event, I first check the change and then call the "e.SkipChange" so that I only detect that there was a change in either the source folder or destination folder, then in the next call of this event I dont call the skip change and carry out the change or synchronization.

How to identify "my" custom folder in an Outlook AddIn

I'm trying to develop an AddIn - or rather, just a proof-of-concept for now, to see if what I have in mind is actually even possible - for Outlook (2010, to be exact), in .NET/C# and I'm facing the following problem:
The AddIn is supposed to offer a new custom Folder (on the top level of the hierarchy, i.e. next to all the other main items, like Tasks, Calendar, Contacts, etc.) in which to offer items to the user. So I figured that in the Startup method of the AddIn I could simply do something like
Outlook.Folder parent = inBox.Parent as Outlook.Folder;
Outlook.Folder myCustomFolder = (Outlook.Folder)parent.Folders.Add("My Custom Folder");
... and that does in fact work. However, there's a problem after quitting Outlook and starting it again. Since the folder is being persisted by Outlook, it is still there the next time Outlook launches and initializes the AddIn again, so the creation of the folder fails because an object of the same name already exists. But I don't see any way how to tell that this is "my" folder from last time.
I don't want to rely on its name to identify the folder (that's just too unreliable to even consider; users might want to rename it, other AddIns might exist that create a folder of the same name, not to mention localization problems etc.), but what else can I use to determine that the custom folder has already been created?
I would either have to be able to somehow add a "tag" do the folder so I can later recognize it as "mine" - or alternatively would need some kind of id that uniquely identifies the folder (and which remains constant even between launches of Outlook!) so I can recognize it by that.
I have been looking at the EntryID and StoreID fields of the [MAPI]Folder object, but from the (sadly, not very detailed) description at the MSDN, I'm not sure if I can rely on them, because apparently they can change under certain conditions.
Any suggestions?
Unless the folder is deleted and then recreated, the entry won't change. But it won't be the same folder anyway - just another folder that might have the same name.
What Outlook does is store the special folders' entryids on the root IPM folder and/or the Inbox folder.
Since you cannot set named properties on folder in Exchange and you cannot just pick your own property tag without risking running into a conflict, create a hidden message in the Inbox folder(which is always present in the default store) and store the folder entry id along with whatever else configuration properties you might need. To make sure your config hidden message is unique, pick a unique mesage class, e.g. IPM.Note.MyCompany.MyAdddin.Config.
Hidden messages can be accessed using MAPIFolder.GetStorage in the Outlook Object Model or RDOFolder.HiddenItems in Redemption.

C# Detect file moves trough different folders

I want to detect every filechanges on a specific folder (except data changes). I decided to use System.IO.FileSystemWatcher to manage that.
//
// fileSysWatchFile
//
this.fileSysWatchFile.EnableRaisingEvents = true;
this.fileSysWatchFile.IncludeSubdirectories = true;
this.fileSysWatchFile.NotifyFilter = System.IO.NotifyFilters.FileName;
this.fileSysWatchFile.SynchronizingObject = this;
this.fileSysWatchFile.Created += new System.IO.FileSystemEventHandler(this.fileSysWatchFile_Created);
this.fileSysWatchFile.Deleted += new System.IO.FileSystemEventHandler(this.fileSysWatchFile_Deleted);
this.fileSysWatchFile.Renamed += new System.IO.RenamedEventHandler(this.fileSysWatchFile_Renamed);
As far as good... New files are detected. File deletes are detected. File renames are detected.
When I move a file to a subfolder it detects first a file delete and then a new file create.
I'd expect that a move is the same as a rename except the path. Seems that it isn't. Can I detect file moves in a save way?
By the way... I only want to detect file changes and not directory changes.
Edit:
Additional Info why I have to detect moves and can't live with delete, create:
I want to replay the same changes on an other drive. If I get a delete first, I delete the shadow file. Then I get the create file event and the original file is already lost :-(.
So I have a drive A which is the watched drive...
And a drive B which has files with the same filenames.
All file changes exept data changes should be replayed on drive B.
The File delete / File create functionality is what is behind a file move. It's similar to rename if you are just moving a file from a folder to a folder, but what about if moving a file from one disk to another, or moving a file between machines?
And, If I'm watching a specified folder, as long as the file is not there, it might as well have been deleted :)
If you are sure that you want to catch file moved "events" (from a watched folder to a watched subfolder), I would maintain a list of recently deleted files, and upon every file created event, check if the file is it that list, indicating a de facto file move.
You can use file system filter driver to track file rename operation. Actually, FS Filter is better approach, than FileSystemWatcher. FileSystemWatcher doesn't provide reliability and flexibility for certain cases (you can see the number of questions regarding FileSystemWatcher and it's glitches and limitations).
FS Filter lets you track the requests as soon as they reach the file system.
You can write your own filter driver, or use our CallbackFilter product.
It's not a real solution of the problem, but I managed to have a quick and dirty solution:
First I am buffering all events for a while (tested with 100ms but we will see how fast it can go).
If a event is 100ms in the buffer I check if there is a depending other event also in there. So for Delete I search all creates and for create I search all deletes.
If I find one I replace the two events with only one move event.
There are some riscs with this workarround:
1.) I can't say what comes first, delete or create. Seems that this is every time different
2.) If the delay is to short, the file is deleted and lost :-(
But as long as I have no better solution I have to live with this.

Is there anything unique for file and folder in windows c#?

I am working on files n folder using C#....May I know is there any way to get the UID for file-folders....Till now i was using the full path of the file....But my problem in is renaming of files n folder...which will change the UID for file folder..
Plz is ther any way to do the same
thanks in advance
First, I think you can get clearer responses if you more clearly define what you mean by "working on files."
No, there's nothing like a "UID" for file or directories. But if you wish to dynamically monitor the state of files and directories, and have events raised notifying you when they are moved/changed/deleted, etc. You can use .NET's FileSystemWatcher class.
Using that technique you could start off, for example, with a Dictionary whose key might be some UID or GUID, or whatever, of the form Dictionary<UID,string>, where string might be the original filename. You could then, as you receive events from the FileSystemWatcher, update a second Dictionary<UID,string> where its string might be the changed file path :
I'm not really proposing you specifically use Dictionaries here, but just using them as possible examples of data structures you could create to keep track of certain files by original name/location and by (possibly) changed name/location ... or if they are deleted, copied, etc.
Hey.. there is a sample provided by microsoft.. it is installed in
"Windows Mobile 6 SDK\Samples\PocketPC\CPP\win32\FileChangeNotif"
location of your hard drive..
through filechangenotif smaple you will get the information about file change notification like
Renaming file,Deletion, addition...hope this can help you
Sorry, there's nothing you can reliably trace that's retained after a file/folder is moved or renamed. Your only real option is to keep track of the renames or simply tell the user that the file's not there anymore.

C# Detecting server setup

I'm writing a program that deals with the logs generated by the clients server. How can I detect where the user is storing them? It feels invasive to search all files, but what if they're being stored outside of the root. Is this acceptable, what if I make the user click "detect" first? Regardless, what if they've been renamed and reformatted? Is it possible to read the server settings themselves from my external program? I want this to work on linux and windows servers. I need WC3 Extended format w/ several fields enabled that are not naturally. I also don't want it to return null if it's enabled but no log has been yet created. I don't want to force the user (assumed dumb) to play with settings.
Any ideas?
Hardcode where you expect them to be in the common case, and if they're not there, ask the user about it. Doing more "magic" than that seems like a recipe for over-complexity and mistakes.
If the user is specifying the location of the log file, then either you should have the user locate the file(s) themselves or keep track of these locations somewhere else when they are saved. You don't need to be doing a full (or large partial) drive search.

Categories

Resources