简体   繁体   中英

how to delete previous uploaded file from directory if user upload a new file in asp.net

I am new to asp.net I have upload file control in .aspx page it works fine. However, I want to delete previous uploaded file from directory if user uploaded a new file.

So this is the abstract code of my code behind file:

public partial class mypage : System.Web.UI.Page
{
   //I cannot access upload label control value here
    string lblValue = lbluploadFile.Text   //this global variable doesn't work

 protected void Page_Load(object sender, EventArgs e)
 {
   // I can access asp.net label control value in page load

   lbluploadFile.Text

 }

 protected void btnAttachment_Click(object sender, EventArgs e)
 {
  File.Delete(ConfigurationManager.AppSettings["UploadFolder"].ToString() + lblValue);
 }

}

How to delete all files and folders in a directory?

you need to get file first with server path (physical path) then you can perform any operation like delete move or change meta data.

combine server path (directory) where you uploaded file with the name of file to get file.

if you need to upload new file in same folder and you want to delete previous one then shortest solution would be just upload new file with the previous name (considering you are uploading same mime type extension file again in case if name is different you can change to previous name) it will auto replace the previous file.

for Ref: How to: Copy, Delete, and Move Files and Folders

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM