Folders not deleting immediately

J

jawloc

Hi I have a question that I hope someone can help me with. I have
created a web page that allows a user to upload an image with a
caption to my web server. Here is what happens when the user loads
the page:


1 - A temporary folder is created with that user's username here is an
example of the folder "c:\wwwroot\upload\tmp\username".

2 - The user uploads the file it gets saved in the temp folder.

3 - The user hits the save button and the caption is dumped to the
database and then a new permanent folder is created (example "c:
\wwwroot\upload\article_43"). So now the file stored in the temp
folder gets copied to the permanent folder and then the content of the
temp folder is deleted along with the temp folder.

This is where I run into a problem. My temporary folder does not
delete immediately if at all. But it does not give me an error. If I
try to do anything with that folder from that point on I get an access
denied error. If I stop the IIS server the temp folder disappears and
everything is as it should be.

Either I'm not using the proper code to create or delete my files and
folders or its something to do with IIS or my web.config.

Here is my helper subs I use to create copy and delete.
================================================================

Public Sub CheckPath(ByVal Path As String)
Dim Mydirectory As New DirectoryInfo(Path)
If Not Mydirectory.Exists Then
Mydirectory.Create()
End If
End Sub

Public Sub CopyDir(ByVal FromPath As String, ByVal ToPath As
String)
Dim toDir As New DirectoryInfo(ToPath)
Dim fromDir As New DirectoryInfo(FromPath)
If toDir.Exists Then
toDir.Delete(True)
toDir.Create()
Else
toDir.Create()
End If
If fromDir.Exists Then
For Each theFiles As FileInfo In fromDir.GetFiles
Dim TargetFile As String = ToPath + "\" +
theFiles.Name
theFiles.CopyTo(TargetFile, True)
Next
End If
End Sub

Public Sub DelDir(ByVal DirPath As String)
Dim DelDir As New DirectoryInfo(DirPath)
If DelDir.Exists Then
DelDir.Delete(True)
End If
End Sub

Public Sub delFile(ByVal FullPath As String)
Dim TargetFile As New FileInfo(FullPath)
If TargetFile.Exists Then
TargetFile.Delete()
End If
End Sub

================================================================

Any help would be apreciated.

Thanks in advance
Eric
 
G

Guest

Hi I have a question that I hope someone can help me with. I have
created a web page that allows a user to upload an image with a
caption to my web server. Here is what happens when the user loads
the page:

1 - A temporary folder is created with that user's username here is an
example of the folder "c:\wwwroot\upload\tmp\username".

2 - The user uploads the file it gets saved in the temp folder.

3 - The user hits the save button and the caption is dumped to the
database and then a new permanent folder is created (example "c:
\wwwroot\upload\article_43"). So now the file stored in the temp
folder gets copied to the permanent folder and then the content of the
temp folder is deleted along with the temp folder.

This is where I run into a problem. My temporary folder does not
delete immediately if at all. But it does not give me an error. If I
try to do anything with that folder from that point on I get an access
denied error. If I stop the IIS server the temp folder disappears and
everything is as it should be.

Either I'm not using the proper code to create or delete my files and
folders or its something to do with IIS or my web.config.

Here is my helper subs I use to create copy and delete.
================================================================

Public Sub CheckPath(ByVal Path As String)
Dim Mydirectory As New DirectoryInfo(Path)
If Not Mydirectory.Exists Then
Mydirectory.Create()
End If
End Sub

Public Sub CopyDir(ByVal FromPath As String, ByVal ToPath As
String)
Dim toDir As New DirectoryInfo(ToPath)
Dim fromDir As New DirectoryInfo(FromPath)
If toDir.Exists Then
toDir.Delete(True)
toDir.Create()
Else
toDir.Create()
End If
If fromDir.Exists Then
For Each theFiles As FileInfo In fromDir.GetFiles
Dim TargetFile As String = ToPath + "\" +
theFiles.Name
theFiles.CopyTo(TargetFile, True)
Next
End If
End Sub

Public Sub DelDir(ByVal DirPath As String)
Dim DelDir As New DirectoryInfo(DirPath)
If DelDir.Exists Then
DelDir.Delete(True)
End If
End Sub

Public Sub delFile(ByVal FullPath As String)
Dim TargetFile As New FileInfo(FullPath)
If TargetFile.Exists Then
TargetFile.Delete()
End If
End Sub

================================================================

Any help would be apreciated.

Thanks in advance
Eric

Looks like the file or directory is locked somewhere and you cannot
delete it until the server is restarted.

Try to delete all files from the directory one by one. Maybe it will
show you if there any problem with lock, or access...

So, instead of

DelDir.Delete(True)

this code

For Each fn As String in Directory.GetFiles(DirPath)
File.Delete(fn)
Next
DelDir.Delete
 
J

jawloc

Thanks for the reply Alex. I've tried what you have suggested and
the files themself aren't locked they delete no problem. If i skip
the deletion of the folder itself everything works ok. I just dont
want to have a million empty folders after running the site for any
extended period of time. It's only the folders that seems locked.
Even if browse to the folder with windows explorer and I select it I
immediately get an access denied error.

Perhaps there is some type of auditing or health monitoring that is
being applies to the upload folder? If so is there a way to remove the
auditing from the upload folder and it's subfolders?

One thing I do know for sure is that its not an NTFS rights thing
cause it happens even on a fat32 system.

Any thoughts are appreciated.
Eric
 
G

Guest

Thanks for the reply Alex. I've tried what you have suggested and
the files themself aren't locked they delete no problem. If i skip
the deletion of the folder itself everything works ok. I just dont
want to have a million empty folders after running the site for any
extended period of time. It's only the folders that seems locked.
Even if browse to the folder with windows explorer and I select it I
immediately get an access denied error.

Perhaps there is some type of auditing or health monitoring that is
being applies to the upload folder? If so is there a way to remove the
auditing from the upload folder and it's subfolders?

One thing I do know for sure is that its not an NTFS rights thing
cause it happens even on a fat32 system.

Any thoughts are appreciated.
Eric

I found the reason.

This is due to File Change Notifications (FCN) and affected the .NET 2
only

http://blogs.msdn.com/toddca/archive/2005/12/01/499144.aspx
http://www.vikramlakhotia.com/Deleting_Directory_in_ASPnet_20.aspx

So, don't delete directories at the runtime

By the way. If this is not working as expected, why don't you change
the logic? You can have only one initial directory at c:\wwwroot\upload
\tmp\ and use it for all files from all users. To remember what the
user has uploaded you can use either session, or a database, or a
special naming of the files (e.g. article_43_thefilename.jpg). Once a
new permanent folder is created you delete all files from the current
user...
 
P

Patrice

Another option also is to don't do this under the application root. Or to
use a virtual directory.

This way ASP.NET 2.0 won't consider that you changed the application storage
layout...
 
J

Jawloc

Thank you both for your help.

I do belive you are correct about the File Change Notification causing
this issue.

I will try the virtual directory thing first and if that gives a hard
time i guess i will need to put a few things back to the drawing board
and use one temp forlder for everyone.

Again thanks for your help,
Eric.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top