Home c# Standing folder monitoring implemented on C #

Standing folder monitoring implemented on C #

Author

Date

Category

Good afternoon. I am trying to write a tiny program on C #, which will monitor the activity of the specified folder and display information about the time of the last change in Label. Tried to implement this process through while:

Public Partial Class Form1: Form
{
  BOOL START = TRUE;
  Public Form1 ()
  {
    Initializecomponent ();
  }
  Private Void Form1_Load (Object Sender, Eventargs E)
  {
    button1.text = ("Start check");
    button2.text = ("Stop check");
  }
  Private Void Button1_Click (Object Sender, Eventargs E)
  {
    While (Start == False)
    {
      DirectoryInfo Dir = New DirectoryInfo (@ "C: \ Users");
      label1.text = ("Full Way:" + dir.fullName);
      label2.text = ("Last updated:" + dir.lastwritetime);
    }
  }
}

But this code example does not make anything at all.


Answer 1, Authority 100%

Link:

Here is the link to Acquaintance with Event Observation File Systems !

There it is written about the observation of the file system events and there are examples.

Filesystemwatcher.changed – event occurs when a file or directory changes in the specified path Path .

FileSystemWatcher.Renamed – event occurs when renaming a file or directory in a given way Path .

Changed event occurs unexpectedly when renaming the file, but it does not occur when changing the name of the catalog.

To track rename, use the Renamed event.

Example:

Public Class Watcher
{
  Public Static Void Main ()
  {
    Run ();
  }
  [PermissionSet, Name = "FullTrust")]
  Public Static Void Run ()
  {
    String [] Args = System.Environment.getCommandLineArgs ();
    // If A Directory Is Not Specified, Exit Program.
    if (args.length! = 2)
    {
      // Display The Proper Way to Call The Program.
      Console.WriteLine ("Usage: Watcher.exe (Directory)");
      Return;
    }
    // Create a New FileSystemwatcher and Set Its Properties.
    FileSystemwatcher Watcher = New FilesystemWatcher ();
    watcher.path = args [1];
    / * Watch for Changes in LastAccess and Lastwrite Times, and
      The Renaming Of Files Or Directories. * /
    watcher.notifyFilter = NotifyFilters.lastaccess | Notifyfilters.lastwrite.
      | NotifyFilters.Filename | Notifyfilters.directoryName;
    // Only Watch Text Files.
    watcher.filter = "* .txt";
    // Add Event Handlers.
    watcher.changed + = new filesystemEventHandler (OnChanged);
    watcher.created + = New FileStemeventHandler (OnChanged);
    Watcher.Deleted + = New FilesystemEventHandler (OnChanged);
    Watcher.Renamed + = New RenameDeventHandler (Onrenamed);
    // Begin Watching.
    watcher.enableraisingevents = true;
    // Wait for the user to Quit the Program.
    Console.WriteLine ("Press \ 'Q \' To Quit The Sample.");
    While (Console.read ()! = 'Q');
  }
  // Define The Event Handlers.
  Private Static Void OnChanged (Object Source, FileSystemEventargs E)
  {
    // Specify What Is Done When A File Is Changed, Created, or Deleted.
    Console.WriteLine ("File:" + E.fullPath + "" + E.ChangeType);
  }
  Private Static Void Onrenamed (Object Source, RenameDeventargs E) 
{
     // Specify what is done when a file is renamed.
     Console.WriteLine ("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
   }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions