Click here to Skip to main content
14,028,484 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi All,
I have web application in C#. Main use of that application is generating reports. application creating temp files while generating reports. I want to delete top 100 files from the Temp folder. Challenge is i cannot delete the files due to file being used by some other process. please help me

What I have tried:

i cannot delete the files due to file being used by some other process. please help me
Posted
Updated 27-Nov-18 22:52pm
Comments
David_Wimbley 28-Nov-18 2:06am
   
You need to post your code, not all of it but the relevant portions in order for anyone to even begin to provide you some guidance.

Likely, you've got an issue where your app has a lock on your files and it hasn't been disposed of properly. Given i can't see your code nor do i have access to your machine, thats the best guess i've got.
riswanzaman 28-Nov-18 3:44am
   
For i As Integer = mcount To mDirectoryInfo.GetFiles().Length - 1
Dim f As FileInfo = mDirectoryInfo.GetFiles()(i)
If f.Extension = ".tmp" Or f.Extension = ".rpt" Then
File.Open(f.FullName, FileMode.Open)
Dim swWriter As New StreamWriter(f.FullName)
swWriter.WriteLine("babababa")
If f.CreationTime.AddMinutes(5) < DateTime.Now Then
' System.GC.Collect();
'System.GC.WaitForPendingFinalizers();
'File.Delete(picturePath);
System.GC.Collect()
System.GC.WaitForPendingFinalizers()
File.Delete(f.FullName)
Console.WriteLine("Deleted")
End If
End If
Next
Vincent Maverick Durano 28-Nov-18 2:08am
   
Can we see your code? Disposing the file before deleting should resolved that issue.
riswanzaman 28-Nov-18 4:40am
   
For i As Integer = mcount To mDirectoryInfo.GetFiles().Length - 1
Dim f As FileInfo = mDirectoryInfo.GetFiles()(i)
If f.Extension = ".tmp" Or f.Extension = ".rpt" Then
File.Open(f.FullName, FileMode.Open)
Dim swWriter As New StreamWriter(f.FullName)
swWriter.WriteLine("babababa")
If f.CreationTime.AddMinutes(5) < DateTime.Now Then
' System.GC.Collect();
'System.GC.WaitForPendingFinalizers();
'File.Delete(picturePath);
System.GC.Collect()
System.GC.WaitForPendingFinalizers()
File.Delete(f.FullName)
Console.WriteLine("Deleted")
End If
End If
Next
Richard Deeming 28-Nov-18 10:58am
   
Calling GetFiles() multiple times like that is extremely inefficient, and liable to cause errors - especially when you're trying to delete some of the files within the loop.

Replace the For loop and its first line with:
For Each f As FileInfo In mDirectoryInfo.GetFiles()
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The chances are that it's your app that has locked the file: if you are creating the file by opening a write stream, then until your app closes or the stream is closed and disposed, the file is locked and no-one else can access it - not even other code in your own app.
The best way to handle this is to create your stream in a using block and do all the work of filling it within that block:
using (StreamWriter sw = new StreamWriter(pathToFile))
   {
   WriteFile(sw);
   }
The using block will automatically flush and close the file, then Dispose of the stream when it is exited - regardless of how it is exited. So a code failure that is caught by a higher level try...catch block for example will stiopp result in the file being saved and unlocked.
   
Comments
CS2011 28-Nov-18 4:15am
   
believe me he has not clue about what you are taking about in your solution. See his code in the comment.
OriginalGriff 28-Nov-18 4:37am
   
Oh dear.
That's ... um ... quite poor isn't it?

I'd better have a word with the poor chap.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Normally, I wouldn't post a second solution, but in this case, now you have posted your code there is a lot to discuss here...

First off, that isn't C# code - it's VB. Always tag your language correctly or you will confuse yourself when the replies don't work or even compile!

Secondly - and I hate to say it - that code looks like it was thrown together by someone who hadn't thought at all, just hoped it would do something, anything, compile-you-bugger, run, oh-it-don't-work, panic.

You have to look at what methods do and what they return, and think about how you need to use them.
You are having a problem that a file is in use: of course it is - you open it twice and don't close either of them!
File.Open(f.FullName, FileMode.Open)

Start with the documentation of File.Open: File.Open Method (System.IO) | Microsoft Docs[^] and it says very clearly what it does:
Quote:
Returns
FileStream
A FileStream opened in the specified mode and path, with read/write access and not shared.
So why do you ignore that FileStream and try to create your own in the next line?
Dim swWriter As New StreamWriter(f.FullName)
That's going to fail, because you have just opened the file for exclusive access in the preceding line!
Replace the two lines with this:
Using swWriter As StreamWriter = File.Open(f.FullName, FileMode.Open)
    ...
End Using
And put your code to write the file inside where the ellipsis is.
The Using block will automatically close the file when you are finished with it, and the variable swWriter goes out of scope. After the Using block closes, you can delete your file, safe in the knowledge that it is no longer in use.

Give that a try, and see how much further you get.
And please, sit down and think before you rush into code - it will save you a huge amount of time and effort!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190419.4 | Last Updated 28 Nov 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100