Solved

Deleting a file using Windows Service in .NET

Posted on 2008-10-31
7
227 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
  • 4
  • 3
7 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 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 500 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now