Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi can anyone tell me how i can determine during runtime whether the User is copying or deleting?

 

Situation is that i have a filewatcher and a timer class which both check a certain directory (my Docs in my case) for its size. So while the size is smaller than a certain value (determined in the registry), it should only check for new incoming files and if there is a change in directory size it should get the size of the whole directory and compair it to that value.

 

If the size is bigger that value, it should check every X seconds (value again determined in registry) if the size is still to big and give out a warning.

 

Problem is:

 

When i'm deleting files while the the method checks for directory size it happens that he gets a file not found error.

 

So i need to find out whether the user is actually copying or deleting before i start my method.

 

Else i need to find another way somehow.

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using Microsoft.Win32;

namespace MyDocMon
{

 public class MyDocMonitor : System.Windows.Forms.Form
 {
   private string _sMyDocPath = Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").
     OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("Shell Folders").
     GetValue("Personal").ToString();
 
   private int _iShortIntervalReg = Convert.ToInt32(Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").
     OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Policies").OpenSubKey("System").
     GetValue("WarnPersonalTimeout"));
   
   private long _lMaxSizeReg = (long)Convert.ToInt32(Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").
     OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Policies").OpenSubKey("System").
     GetValue("MaxPersonalSize"));

   private long _lMaxSize = 0;
   private int _iShortInterval = 0;

   private long _lTotalSize = 0;
   
   private Boolean _bIsTooBig = false;
   private Boolean _bCheckerEnable = false;
   private Boolean _bWatcherEnable = true;

   private Timer _tiReCheck = new Timer();
   private FileSystemWatcher _fswMyDocWatcher = new FileSystemWatcher();
   private Message _aMessage = new Message();
   
   private void Form1_Load(object sender, System.EventArgs e)
   {
     notifyIcon1.Icon = IconWatch;

     _lMaxSize = _lMaxSizeReg *1024 * 1024;
     _iShortInterval = _iShortIntervalReg * 60 *100;
     
     checkSize(null,null);

     _tiReCheck.Interval = _iShortInterval;
     _tiReCheck.Enabled = true;
     _tiReCheck.Tick +=new EventHandler(_tiReCheck_Tick);
     
     _fswMyDocWatcher.Path = _sMyDocPath;
     _fswMyDocWatcher.NotifyFilter = NotifyFilters.Size;
     _fswMyDocWatcher.Filter = "";
     _fswMyDocWatcher.IncludeSubdirectories = true;
     _fswMyDocWatcher.Changed += new FileSystemEventHandler(checkSize);
     _fswMyDocWatcher.EnableRaisingEvents = true;
   }

   private void checkSize2 ()
   {
     _lTotalSize = 0;
     DirectoryInfo aInfo = new DirectoryInfo(_sMyDocPath);
     getDirectorySize(aInfo);
   
     String sTotalSize = Convert.ToString(_lTotalSize /1024 /1024) + " MB";
     String sMaxSize = Convert.ToString(_lMaxSize /1024 /1024) + " MB";
     if(_lTotalSize > _lMaxSize)
     {
       _bIsTooBig = true;
       String sDifferenzSize = Convert.ToString((_lTotalSize - _lMaxSize)/1024 /1024) + " MB";
       
       _aMessage.richTextBox1.Text = "Ihre Eigenen Dateien belegen im Moment " + sTotalSize + " / " + sMaxSize 
         + ".\n\rBitte entfernen Sie "+ sDifferenzSize + ", da es sonst Probleme mit \n\rIhren Dateien geben kann.";
       _aMessage.ShowDialog();
     }
     else
     {
       _bIsTooBig = false;
     }

   }

   private void checkSize(object source, FileSystemEventArgs e)
   {
     if(_bWatcherEnable)
     {
       _bCheckerEnable = false;
       _bWatcherEnable = false;
       
       checkSize2();
       
       if(_bIsTooBig)
       {
         _bCheckerEnable = true;
       }
       else 
       {
         _bWatcherEnable = true;
       }
     }
     return;
   }

   private void _tiReCheck_Tick(object sender, EventArgs e)
   {
     
     if(_bCheckerEnable)
     {
       _bWatcherEnable = false;
       _bCheckerEnable = false;
       
       checkSize2();
       
       if (!_bIsTooBig)
       {
         _bWatcherEnable = true;
       }
       else
       {
         _bCheckerEnable = true;
       }
     }
     return;
   }

   private void getDirectorySize(DirectoryInfo diMyDocInfo)
   {
     foreach(FileInfo aFileInfo in diMyDocInfo.GetFiles())
     {
       _lTotalSize += aFileInfo.Length;
     }  
     foreach (DirectoryInfo aDirInfo in diMyDocInfo.GetDirectories())
     {
       getDirectorySize(aDirInfo);
     }
   }
 }
}

Posted
No reply in ages...

 

i guess this forum is teh dead?

If you actually looked at any other thread in the forums you would see that it is far from dead. Did you ever consider that perhaps people just don't have an answer for you question?

Anybody looking for a graduate programmer (Midlands, England)?
Posted
If you actually looked at any other thread in the forums you would see that it is far from dead. Did you ever consider that perhaps people just don't have an answer for you question?

 

Wasnt meant that seriously ;)

 

I considered it and still waiting for a reply, the one finding an answer to how to find out whether a user is copying maybe knows an answer too to the question why the filesystemwatcher class can check the size of a directory without knowing it ;)

 

regardz

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...