views:

62

answers:

2

I'm trying to delete files created by current user when he/she clicks logout button

Protected Sub OnLoggingOut(ByVal sender As Object, ByVal e As EventArgs) Handles LoginStatus1.LoggingOut
        Try
            Dim folder As String = Server.MapPath("~/uploads/")
            Dim files As String() = Directory.GetFiles(folder)

            For Each f In files
                Dim filename As String = Path.GetFileName(f)
                If filename.Contains(HttpContext.Current.User.Identity.Name) Then
                    File.Delete(filename)
                End If
            Next
        Catch ex As Exception
            LogFile(ex.Message(), DateTime.Now)
        End Try
    End Sub

This event gets executed/called and even file.delete but files don't get deleted. Is there something wrong with this code or server doesn't execute any server methods on logging out?

How can i delete files when session ends ? It gives me error saying server methods cannot be executed

+1  A: 

You say that you have traced execution and the right methods and files get called, but not actually deleted?

You should be getting more detailed error messages. What's in your LogFile?

Right off the bat, it sounds like your ASP.NET processes on the server may not have the proper permissions to do this (they require more permissions than the default installation, if I remember correctly).

I've seen a very odd problem elsewhere that if the file is "old", for us, this was ~12< hours, the process would be unable to delete the file, but for newly created files, it deleted them just fine. If this is the case, I'm not sure what to tell you, perhaps you can find a work-around.

Lastly, as @Aristos suggested, you should check to make sure that the username is not null. It's not uncommon for users to time out, press back button, etc, and cause ASP.NET to "forget" things like Session or Application variables.

rlb.usa
I don't get any excepting when I'm trying to debug this, so was not able to solve this, using sessions solved my issue. But I'm still not able to execute this in session end. It doesn't allow any server methods like server.mappath, directory.getinfo
rs
If you are so locked down as to not be able to read directory information, you are probably locked out of file I/O operations as well. Until you contact someone above yourself, you're stuck, my friend.
rlb.usa
+1  A: 

seems like a complicated way of doing things..

do they files have to be deleted right at end of user session?

why don't you write a small program that watches that directory, and deletes any files that are longer than n period. (session length, or 24hrs or whatever).

number of utilities exist, and .net great support for it as well.. than you don't have to worry about orphaned files because application restarted and user session events didn't fire correctly.. also would be more secure as your asp.net application wouldnt need IIS directory write access. just small standalone windows service would handle it.

Sonic Soul
user uploads a file uses it to create and send email and then on logging out app should delete it or else I'll have old files. In session_start i'm checking and deleting any files older than a day. But this event will only execute when someone access this app again. Your idea sounds interesting in this case, but deleting based on session length is tricky becos user session can exceed default value
rs
yes, but with a directory listener, it will delete all old files. they don't have to disappear RIGHT after user leaves.. and you don't have to rely on session end event, which is not 100% reliable. for example if your server or IIS bounces , it doesn't fire session end events for all sessions.. than you're left with orphaned files.
Sonic Soul
yes i know session end is not reliable. I might create a service that will delete all files every 24hrs thanks for suggestion. I'll keep this question open for few more hours for other users views
rs