?
Solved

Deleting a file using Windows Service in .NET

Posted on 2008-10-31
7
Medium Priority
?
233 Views
Last Modified: 2013-11-18
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
0
Comment
Question by:Sreejith22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 22855748
Hi Sreejith22,

You need to release the file from the parsing routine before you can delete it.

Can you post your parsing routine here, or at least tell us how you are opening the file?
Regards,

Wayne
0
 

Author Comment

by:Sreejith22
ID: 22855847
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
0
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 2000 total points
ID: 22855860
Could you also post the GetStoriesFromRSSFeed routine, because this is the one that does the opening and parsing of the file.

At the very least, we'll need to know how the file is opened for parsing. This info will let us know what objects you are using, and we'll be able to provide concise instructions for releasing the file from memory.

Wayne
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:Sreejith22
ID: 22855912
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
0
 

Author Closing Comment

by:Sreejith22
ID: 31512266
Thanks a million Wayne
0
 

Author Comment

by:Sreejith22
ID: 22856057
Wayne,
Thanks a lot.
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 22856136
No problems, Sreejith. Glad to help :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question