简体   繁体   中英

ASP.NET Deleting files on the file system?

As part of some code logic, the component downloads the user uploaded files to a specific directory.

I am using

System.IO.File.Delete(file1);
System.IO.File.Delete(file2);

to delete the files. I don't think the local system account IUSR account has permissions to do that.

What are the best practices to handle deletion of files in ASP.NET?

  1. Make a folder that is outside your web application to store the files (ie when you republish the site, it is not affected)
  2. Give that folder appropriate permissions for the IUSR account (or whatever application pool identity you're running under)
  3. Store that folder's path in your web.config so you don't have to change code for it.

Delete the file before saving it will refresh automatically

System.IO.File.Delete(filepath); fupedit.SaveAs(Server.MapPath(filePath));

if you need to delete the file, the ASP.NET process account (NETWORK SERVICE when on Server 2003 and Vista) must be granted the proper permissions to remove files.

I typically will have a "processing" folder that way I can grant proper permissions just to that directory.

I run asp.net sites using Impersonation and authenticate under a trusted domain account user. Then give that user write permissions on the folder that contains the files you want to delete.

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