Reputation: 14011
I want to write a program for when I open the file and view the file which is in e:\test\test.doc then I want to dispaly the path in separate file filetracing.txt.
If I open the file in f:\doc.pdf the output should be:
the file in "e:\test.doc" is opened on 2.30 am
the file in "f:\doc.pdf" is opened on 8.30 am like wise
How do I write the program for this in c#?
Upvotes: 0
Views: 813
Reputation: 165
You can make a small monitoring application which can run only with a notify icon UI and implement the FileSystemWatcher class to watch the files and raise logging event when necessary
An example of usage of FileSystemWatcher is below
Upvotes: 0
Reputation:
Did you mean that you want the application to write to the logfile when any file on c:\ is accessed?
If so, then there's a possible solution involving the System.IO.FileSystemWatcher class. (I'd link to the MSDN Docs, but since I'm new here, I can't)
The FileSystemWatcher class is intended more for watching for changes to the filesystem, rather than any direct filesystem access. Therefore with this solution you're limited to watching for changes to LastAccessed - this is not always updated, and thus should be relied upon for strict auditing reasons.
Here's a quick piece of code to demonstrate using FileSystemWatcher to monitor for LastAccessed changes:
using (var w = new System.IO.FileSystemWatcher("c:\\"))
{
w.IncludeSubdirectories = true;
w.NotifyFilter = NotifyFilters.LastAccess;
w.Changed += (object sender, FileSystemEventArgs e) =>
{
Console.WriteLine("{0} {1} at {2}", Path.Combine(e.FullPath, e.Name), e.ChangeType, DateTime.Now);
};
w.EnableRaisingEvents = true;
Console.WriteLine("Press Enter to exit");
Console.Read();
}
Notes:
This gets events when any file has it's LastAccessed property updated - that includes file Creation and Modification.
If you log to the same drive as you are monitoring, you will get your own Write events - so don't forget to filter them.
Upvotes: 1
Reputation: 1641
using System.IO;
string path ="e:\test\test.txt"
// save your path in a string
FileStream file = new FileStream(path, FileMode, FileAccess); //opens the test.doc file
// perform the file operation
file.Close();
//open the path file
FileStream pathfile = new FileStream("filetracing.txt",FileMode,FileAccess);
StreamWriter sw = new StreamWriter(pathfile);
sw.Write(path); // the path string will be written in your filetracing.txt
sw.Close(); pathfile.Close();
Upvotes: 0
Reputation: 974
very quickly ...... you can use opendialog modalbox and can save the path as as opendialogObject.path and the current time part of DateTime.Now() and can put it to display. sorry if that dosen't help
Upvotes: 1