Re: how to delete a file that is related to the session object

From: itay segal (segal_at_discussions.microsoft.com)
Date: 09/19/04


Date: Sun, 19 Sep 2004 12:03:02 -0700

hello john,
i'll put some code samples to make it clearer:
first, i have a static object which is created each time the session start:
                protected void Session_Start(Object sender, EventArgs e)
                {
                        myUser = new user();
                }
this user object is reffered to in my code this way:
                currentUser = (user)Session.StaticObjects["myUser"];
in the user's object constructor, a new file is created, and its name is
stored in
a string field - filePath:
        Random rnd = new Random();
        filePath = @"C:\Inetpub\wwwroot\treeApplication1\treefiles\"+(rnd.Next
(1,10000)).ToString() + ".tre" ;
        File.Copy(@"C:\Inetpub\wwwroot\treeApplication1\treefiles\original.tre",
filePath,false);

as you can see, the object holds a reference to a new file, using its name.
later on in the program, the file will be opened and several action will be
performed (irrenlevant to my issue)

the user object also has a destructor method:

                ~ user()
                {
                                File.Delete(filePath);
                }

now here's my problem: if the application's user closes the window, and his
session ends, the garbage collector doesn't always work at that instance.
which means, the garbage collector will operate this destructor method after
the session object no longer exists, and the file won't be deleted (the
filePath string will hold a null value)
how can i overcome this problem? how can i force the garbage collector to
collect this user object each time the user closes the window (and ends his
session)?
i hope i made myself clear.
thanks again
itay

"John Saunders" wrote:

> "itay" <itay@discussions.microsoft.com> wrote in message
> news:60F10D6E-4A5C-4028-B1C8-06374B874811@microsoft.com...
> > well, i use a static object for every session in my web app., and in that
> > object there's a new file copied from an existing file, and this file is
> > needed as long as the session exists. my question is - if the user closes
> the
> > explorer's window, the session ends, but the file isn't deleted. if i add
> a
> > destructor to the session object, where i try to delete this file - the
> > garbage collection engine will reach it, but won't recognize the file (the
> > file string will be assigned with null value, after the user has shut the
> > window). clearly, if the user preses an "end session" button, there will
> be
> > no problem. but as far as i know, most users will press the "X" button to
> > close the window, and that way i will never be able to delete the file.
> > i'd realy appreciate someone's help with this issue
>
> Your description isn't sufficient to tell me what your code is doing. For
> instance, what do you mean when you say "a static object for each session"?
>
> Maybe you could boil this down to a small reproducer and then post the code?
> --
> John Saunders
> johnwsaundersiii at hotmail
>
>
>