C# .NET - drag and drop event of windows explorer

Asked By Abhishek Tiwari on 04-Sep-09 08:50 AM
Hi.
I need to track the event when someone tries to drag and drop a  file or folder onto and existing folder in windows explorer. Is this possible?

drag and drop event of windows explorer - Sagar P replied to Abhishek Tiwari on 04-Sep-09 09:04 AM

See these links which will give you idea abt wht you are doing;

http://www.daniweb.com/forums/thread166429.html#

http://www.codeproject.com/KB/cs/draginternetshortcut.aspx

http://www.codeproject.com/KB/cs/dragdrop.aspx

Re - Ravenet Rasaiyah replied to Abhishek Tiwari on 04-Sep-09 09:20 AM

Hi

In windows form application we can do the drag and drop features easily.

just take look at this sample

http://www.csharphelp.com/archives/archive263.html

thank you
http://www.codegain.com

You can track the drag and drop event of windows explorer - by monitoring that folder.

[)ia6l0 iii replied to Abhishek Tiwari on 04-Sep-09 09:33 AM
What you are typically trying to achieve is a monitoring tool to watch a folder. 

To watch a file / folder from .Net , you can use the FileSystemWatcher class that is available under the System.IO namespace. 

Sample Snippet below:

private void WatchFolder(string folderPath)
{
//Create a file system watcher.
System.IO.FileSystemWatcher fsWatcher = new FileSystemWatcher();
       //set the file system watcher properties
fsWatcher.Path = folderPath;
//set the notify filter attributes.
        fsWatcher.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
        fsWatcher.NotifyFilter = fsWatcher.NotifyFilter | System.IO.NotifyFilters.FileName;
       fsWatcher.NotifyFilter = fsWatcher.NotifyFilter | System.IO.NotifyFilters.Attributes;
  //add event handlers for changed, and created events of the filesystemwatcher.
       fsWatcher.Changed += new FileSystemEventHandler(fsEventOccured);
      fsWatcher.Created += new FileSystemEventHandler(fsEventOccured);
    
  //enable the FileSystemWatcher to monitor the folder by setting the EnableRaisingEvents property          //to true.
  try
     {
              fsWatcher.EnableRaisingEvents = true;
     }
  catch (Exception ex)
     {
          //
     }
finally
  {
//stop monitoring
fsWatcher.EnableRaisingEvents = false;
  }
}

/// <summary>
/// FileSystem passes the event occured.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
private void fsEventOccured(object sender, System.IO.FileSystemEventArgs e)
{
    switch (e.ChangeType)
    {
        case WatcherChangeTypes.Changed:
            //e.FullPath gives you the file path in the folder.
            break;
        case WatcherChangeTypes.Created:
   //file is created here.
            break;
        default: 
            break;
    }
}   

Loop thru the WatcherChangeTypes Enum for other actions that can occur on the folder.