Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How can I notify the user when the currently active file is deleted from the saved location.

Thank you!
Posted
Updated 5-Dec-10 21:07pm
v2
Comments
Toli Cuturicu 6-Dec-10 3:25am    
"the currently active file"... There is no such thing!
Manfred Rudolf Bihy 6-Dec-10 5:13am    
@Toli: OP is most likely looking at this from the perspective of his application scenario, but to reduce the confusion he should have elaborated on what he means by "currently active file". Or maybe not use the phrase altogether, since it doesn't add any information whatsoever. :)
Manfred Rudolf Bihy 8-Dec-10 7:37am    
Did place that one vote? I hope not as I did answer your question correctly and even had a solution for your follow up question.

Hi Subhash,

I'm not quite sure what you mean by "currently active file", but if you want to get notified of file changes, renames, moves and deletions on way to go is to use the FileSystemWatcher class. It lets you handle all these kind of events.

Example:

C#
class Program
{
    private static FileSystemWatcher fw;
    static void Main(string[] args)
    {
        String path = args[0];
        String filter = args[1];

        fw = new FileSystemWatcher(path, filter);
        fw.Deleted += new FileSystemEventHandler(fw_Deleted);

        fw.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime |
                          NotifyFilters.DirectoryName | NotifyFilters.FileName |
                          NotifyFilters.LastAccess | NotifyFilters.LastWrite |
                          NotifyFilters.Security | NotifyFilters.Size;
        fw.IncludeSubdirectories = true;
        fw.EnableRaisingEvents = true;
        Console.ReadLine();
    }

    static void fw_Deleted(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("{3:yyyy-MM-ddTHH:mm:ss.ffff} Deleted event: {0} {1} {2}", e.Name, e.FullPath, e.ChangeType, DateTime.Now);
    }
}


This console application takes two command line parameters:
1. the path to watch e.g. c:\temp
2. which file extentions e.g. *.txt

Cheers


Manfred
 
Share this answer
 
v3
Comments
subhash04573 6-Dec-10 4:43am    
i getting exception like path is not a legal form.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName(@"D:\");
watcher.Filter = Path.GetFileName(@"D:\help2.xml");

My requirement is i am using this xml file in the application. if the user goes to the location & deletes this xml file means. it should notify that file is deleted.
Manfred Rudolf Bihy 6-Dec-10 4:48am    
Please do it like this: FileSystemWatcher watcher = new FileSystemWatcher(@"D:\","help2.xml");
or like this:
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"D:\";
watcher.Filter = "help2.xml";

For a good explanation of the FileSystemWatcher class and its methods, properties and events and also some examples see the MSDN documentation

That should do it :)
subhash04573 6-Dec-10 5:15am    
namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
Console.WriteLine("Started....");

//watcher.SynchronizingObject = this;

watcher.Path = Path.GetDirectoryName(@"D:\Samples");
watcher.Filter = Path.GetFileName(@"*.xml");
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Size;

watcher.Deleted += new System.IO.FileSystemEventHandler(OnDelete);
watcher.EnableRaisingEvents = true;

Console.ReadLine();

}
public static void OnDelete(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: {0} Deleted",e.FullPath);
}

}
}


it's not raising any event even after deleting file .
Manfred Rudolf Bihy 6-Dec-10 5:18am    
Please read my last comment. I showed you there what to put into the Path and Filter properties. Read my comment and do as you were told!
subhash04573 6-Dec-10 6:02am    
i got it now.
Another thing. Can't we restrict the user for not to delete that file when it is in use.
Heya,

/*--[ Function ]-----------------------------------------------------------*/
/*
  Function Name   : FileExist
  Description     : This function checks whether the file specified
                    by txtFilePath exists and returns file status.

  Access Modifier : public
  Param           : -none-

  Retval          : bool   : true  - file exists
                           : false - no such file
*/
/*-------------------------------------------------------------------------*/
public bool FileExist(string txtFilePath)
{
  bool returnValue;
  FileInfo genericFileInfo = new FileInfo(txtFilePath);
  returnValue = genericFileInfo.Exists;
  return returnValue;
}


You can use this function as an addition to checking whether a file exitst (not deleted) or not(deleted).
Hope this was useful!
Please vote!

Regards,
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900