Solved

Deleting a file using Windows Service in .NET

Posted on 2008-10-31
7
232 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 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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

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 …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

690 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