Link to home
Start Free TrialLog in
Avatar of cimscims
cimscims

asked on

Read XML files and delete

1) I have a folder "ProjectDocuments". XML files are created and stored in the folder with the names as
     - 'Data_07_15_2013_09:55:20.xml'
     - 'Data_07_15_2013_09:57:20.xml'
     - 'Data_07_15_2013_11:57:00.xml' etc.
2) I’m looking for a service/file watches which should check for the files, read and delete the files.

Need help. Thanks in advance
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

use FileSystemWatcher.
for example in your case use this code:
static void CreateWatcher(){
// Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"c:\ProjectDocuments";
        /* 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 xml files.
        watcher.Filter = "*.xml";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
}

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);
    }

Open in new window

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
Avatar of cimscims
cimscims

ASKER

Thanks sedwick for q quick response. Do i have to use the above code in the windows service?
u don't have to but u can.
do u want it to run as a service?
If i don't use service, how should i use the code. Please suggest.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you. I will try and shall let you know.
I've requested that this question be closed as follows:

Accepted answer: 0 points for cimscims's comment #a39326840

for the following reason:

Thank you sedgwick.
I want to close this question by awarding points to sedgwick. Accidently i clicked on my comment as solution.
Thank you sedgwick.