6
Answers

How to delete uploaded images from Temp folder when user is logout using global.asax in asp.net

Photo of sagar Bhosale

sagar Bhosale

13y
2.8k
1
How to delete uploaded images from Temp folder when user is logout using global.asax in asp.net

my global.asax.cs code please help me 

protected void Session_End(object sender, EventArgs e) {

string uId = Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString();
string path;
string[] filename;

path = System.Web.HttpContext.Current.Server.MapPath(@"\Images");

filename = Directory.GetFiles(uId + "_" + path[path.Length - 1]);

for (int i = 0; i < filename.Length; i++) {
if (Session.SessionID == Path.GetFileName(filename[i])) {

string imgpath = filename[i];

FileInfo filepath = new FileInfo(imgpath);

if (filepath.Exists) {

File.Delete(Server.MapPath("~/temp/" + filepath.FullName));

Session.Abandon();
}

}
}

}

Answers (6)

0
Photo of Suthish Nair
NA 31.7k 4.6m 13y
Hope your thread resolved here.. http://www.c-sharpcorner.com/Forums/Thread/127863/how-to-delete-image-according-to-intial-string-of-the-im.aspx
0
Photo of Suthish Nair
NA 31.7k 4.6m 13y
 
 Session.Abandon method destroys all the objects stored in a Session object and releases their  resources. You need write separate logic to delete the physical files.
0
Photo of sagar Bhosale
NA 167 147.2k 13y
sir in this Session.Abandon(); is neccessary for delete images or file
0
Photo of Suthish Nair
NA 31.7k 4.6m 13y

System
.IO.Path.GetTempFileName()
will give you the temp folder with files.

You needs to do
if (File.Exists(sFileNamePath)) { File.Delete(sFileNamePath); }


0
Photo of sagar Bhosale
NA 167 147.2k 13y
Thanks Sir , but i want to do this in global.asax . sir is the above code is correct  anything missing from above code how to delete images from temp folder  when user is logout 
0
Photo of Suthish Nair
NA 31.7k 4.6m 13y

 try this link..