Sreejith22
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
Thanks a million Wayne
ASKER
Wayne,
Thanks a lot.
Thanks a lot.
No problems, Sreejith. Glad to help :)
ASKER
Thanks for the quick response.
Here is the code snippet
private void GetFileName()
{
try
{
string XMLFilePath = ConfigurationManager.AppSe
DirectoryInfo srce = new DirectoryInfo(XMLFilePath)
CheckDirectories(srce);
}
catch (Exception e)
{
//EventLog.WriteEntry(e.Me
}
}
private void CheckDirectories(Directory
{
try
{
string[] folderName;
foreach (DirectoryInfo tempdir in source.GetDirectories())
{
DirectoryInfo tSource = new DirectoryInfo(source.FullN
CheckDirectories(tSource);
folderName = tSource.FullName.Replace(C
CheckFiles(tSource, folderName[1]);
}
}
catch (Exception e)
{
//EventLog.WriteEntry(e.Me
}
}
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(temp
// Once the parsing is completed the tempfile has to be deleted
PhotoMain = "";
PhotoThumbNail = "";
PhotoPreview = "";
PhotoFileName = "";
HasPhotos = false;
}
}
}
}
Sreejith