Пример #1
0
 private void fsChanged(FileSystemEventArgs e)
 {
     // Does all the color stuff
     if (e.ChangeType == WatcherChangeTypes.Deleted)
     {
         DeleteFiles_timer.Stop();
         foreach (ListViewItem item in filelistView.Items)
         {
             if (item.Text == e.Name)
             {
                 item.BackColor = Color.Red;
             }
         }
         DeleteFiles_timer.Start();
         return;
     }
     ClearColor_timer.Stop();
     if (e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Renamed)
     {
         UpdateFileFolerListView(path_txt.Text);
     }
     foreach (ListViewItem item in filelistView.Items)
     {
         if (item.Text == e.Name && item.BackColor == Color.White)
         {
             if (e.ChangeType == WatcherChangeTypes.Created)
             {
                 item.BackColor = Color.LightGreen;
             }
             else if (e.ChangeType == WatcherChangeTypes.Changed)
             {
                 item.BackColor = Color.Yellow;
             }
             else if (e.ChangeType == WatcherChangeTypes.Renamed)
             {
                 item.BackColor = Color.Yellow;
             }
         }
     }
     ClearColor_timer.Start();
 }
Пример #2
0
        /// <summary>
        /// Updates the listview with the contents of a directory at the given path.
        /// </summary>
        /// <param name="path">Path to directory</param>
        private void UpdateFileFolerListView(string path)
        {
            ClearColor_timer.Stop();
            DeleteFiles_timer.Stop();
            if (!path.EndsWith("\\"))
            {
                path += "\\";
            }
            path_txt.Text = path;
            FileAttributes atrib;

            string[]     buff;
            ListViewItem lvi;

            filelistView.Items.Clear();
            //Files
            try
            {
                foreach (string item in Directory.EnumerateFiles(path))
                {
                    buff = item.Split('\\');
                    lvi  = new ListViewItem(buff[buff.Length - 1]);
                    try
                    {
                        lvi.SubItems.Add(File.GetLastAccessTime(item).ToString());
                        atrib = File.GetAttributes(item);
                        lvi.SubItems.Add(atrib.HasFlag(FileAttributes.ReadOnly).ToString());
                        lvi.SubItems.Add(atrib.HasFlag(FileAttributes.Temporary).ToString());
                        lvi.SubItems.Add(atrib.HasFlag(FileAttributes.Hidden).ToString());
                        lvi.SubItems.Add(((new FileInfo(item)).Length / 1000.0).ToString() + " KB");
                        lvi.BackColor = Color.White;
                    }
                    catch (Exception)
                    {
                        lvi.Text = "!#Error";
                    }
                    filelistView.Items.Add(lvi);
                    filelistView.Items[filelistView.Items.Count - 1].Group = filelistView.Groups[1];
                }
                DirectoryInfo dirinfo;
                //Folders
                foreach (string item in Directory.EnumerateDirectories(path))
                {
                    buff = item.Split('\\');
                    lvi  = new ListViewItem(buff[buff.Length - 1]);
                    try
                    {
                        lvi.SubItems.Add(Directory.GetLastAccessTime(item).ToString());
                        dirinfo = new DirectoryInfo(item);
                        lvi.SubItems.Add(dirinfo.Attributes.HasFlag(FileAttributes.ReadOnly).ToString());
                        lvi.SubItems.Add(dirinfo.Attributes.HasFlag(FileAttributes.Temporary).ToString());
                        lvi.SubItems.Add(dirinfo.Attributes.HasFlag(FileAttributes.Hidden).ToString());
                        lvi.BackColor = Color.White;
                    }
                    catch (Exception)
                    {
                        lvi.Text = "!#Error";
                    }
                    filelistView.Items.Add(lvi);
                    filelistView.Items[filelistView.Items.Count - 1].Group = filelistView.Groups[0];
                }
                // Setup the file system watcher
                fswatcher.Dispose();
                fswatcher = new FileSystemWatcher(path)
                {
                    NotifyFilter        = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.DirectoryName | NotifyFilters.Attributes | NotifyFilters.Size,
                    Filter              = "*.*",
                    EnableRaisingEvents = true
                };
                fswatcher.Created += Fswatcher_DoUpdate;
                fswatcher.Deleted += Fswatcher_DoUpdate;
                fswatcher.Renamed += Fswatcher_DoUpdate;
                fswatcher.Changed += Fswatcher_DoUpdate;
            }
            catch (Exception)
            {
                fswatcher.Dispose();
                MessageBox.Show("Could not complete the operation.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }