Answered by:
Download or Delete files with given full path filename

Question
-
User-374095980 posted
Dear all,
If I have the file name full path (e.g. C:\temp\abc.csv), How can I write the code to delete the file or download the file in the code behind??
Wednesday, May 20, 2009 4:46 AM
Answers
-
User-1419202310 posted
To Download:
string filename = "D:\\users\\Abc.txt"; if (filename != "") { System.IO.FileInfo file = new System.IO.FileInfo(filename); if (file.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(file.FullName); Response.End(); } else { Response.Write("This file does not exist."); } }
To Delete:
FileInfo fi = new FileInfo(filename); fi.Delete();
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 20, 2009 6:02 AM
All replies
-
User604186779 posted
If I have the file name full path (e.g. C:\temp\abc.csv), How can I write the code to delete the file or download the file in the code behind??Check this link...
Wednesday, May 20, 2009 5:21 AM -
User-1327195235 posted
Hi,
Try following code to delete file
using System.IO;
public void DeleteImageFile(string filename)
{
try
{
string filePath = filename;
FileInfo fi = new FileInfo(Server.MapPath( filePath));
fi.Delete();
}
catch (Exception ex)
{
//throw ex;
}
}
Wednesday, May 20, 2009 5:23 AM -
User-1085149042 posted
Try this.
File.Delete(filepath);
Wednesday, May 20, 2009 5:24 AM -
User-1419202310 posted
To Download:
string filename = "D:\\users\\Abc.txt"; if (filename != "") { System.IO.FileInfo file = new System.IO.FileInfo(filename); if (file.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(file.FullName); Response.End(); } else { Response.Write("This file does not exist."); } }
To Delete:
FileInfo fi = new FileInfo(filename); fi.Delete();
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 20, 2009 6:02 AM