Link to home
Start Free TrialLog in
Avatar of Sreejith22
Sreejith22Flag for India

asked on

Deleting a file using Windows Service in .NET

I have a windows service created for parsing XML files contained in certain folders. The folders are organised into categories

eg: D:\Feeds\Business
D:\Feeds\Entertainement
D:\Feeds\Politics etc

These folders will contain XML files. My parser program will loops though all the files in these folders, parses the XML data and inserts into the database. My aim is to delete these files once the parsing is completed. When i try to execute the file deleting code inside the windows service an error message "cannot delete the file..it is being used by another process" is shown. Is there any way i can delete these files from the windows service.
Regards,
Sreejith
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia 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
Avatar of Sreejith22

ASKER

Hi Wayne,

Thanks for the quick response.

Here is the code snippet

  private void GetFileName()
        {

            try
            {
                string XMLFilePath = ConfigurationManager.AppSettings["XMLFilePath"].ToString();
                DirectoryInfo srce = new DirectoryInfo(XMLFilePath);
                CheckDirectories(srce);
            }
            catch (Exception e)
            {
                //EventLog.WriteEntry(e.Message.ToString());
            }
        }


 private void CheckDirectories(DirectoryInfo source)
        {
            try
            {
                string[] folderName;

                foreach (DirectoryInfo tempdir in source.GetDirectories())
                {
                    DirectoryInfo tSource = new DirectoryInfo(source.FullName + "\\" + tempdir.Name);
                    CheckDirectories(tSource);
                    folderName = tSource.FullName.Replace(ConfigurationManager.AppSettings["XMLFilePath"].ToString(), "").Split('\\');
                    CheckFiles(tSource, folderName[1]);
                }
            }
            catch (Exception e)
            {
                //EventLog.WriteEntry(e.Message.ToString());
            }
        }

private void CheckFiles(DirectoryInfo source, string FolderName)
        {
            if (FolderName != "Video")
            {
                string extension = "";
                foreach (FileInfo tempfile in source.GetFiles())
                {
                    extension = tempfile.Extension.ToLower();
                    if (extension == ".xml")
                    {
                        GetPhotoDetails(source, tempfile.Name, FolderName);
                        GetStoriesFromRSSFeed(tempfile.FullName, FolderName);//This routine will do the parsing
                       // Once the parsing is completed the tempfile has to be deleted
                        PhotoMain = "";
                        PhotoThumbNail = "";
                        PhotoPreview = "";
                        PhotoFileName = "";
                        HasPhotos = false;
                    }
                }
            }
        }

Sreejith
SOLUTION
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
Hi Wayne,

You are right.
I was using an XmlTextReader inside the parsing routine which i didm't close after the parsing is done. Now when i closed the XmlTextReader, i am able to delete the fiiel

Thanks
Sreejith
Thanks a million Wayne
Wayne,
Thanks a lot.
No problems, Sreejith. Glad to help :)