Srikar Doddi
Srikar Doddi

Reputation: 15599

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?

Upvotes: 0

Views: 2359

Answers (4)

Sunny Gambhir
Sunny Gambhir

Reputation: 11

Delete the file before saving it will refresh automatically

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

Upvotes: 1

David
David

Reputation: 15350

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.

Upvotes: 0

Brandon
Brandon

Reputation: 14196

  1. Make a folder that is outside your web application to store the files (i.e. 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.

Upvotes: 5

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

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.

Upvotes: 0

Related Questions