Solved

Deleting a file using Windows Service in .NET

Posted on 2008-10-31
7
230 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

809 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