C# .NET - Delete file using URL format

Asked By Ganesan BalaKrishnan on 06-Aug-09 09:12 AM
How to delete file using URL format in C#?

Are you talking about

Santhosh N replied to Ganesan BalaKrishnan on 06-Aug-09 09:16 AM

deleting a file using asp.net...

then you can do this way

protected void btnSubmit_Click(object sender, EventArgs e)
{

try {
FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + txtFile.Text);
if (TheFile.Exists) {
File.Delete(MapPath(".") + "\\" + txtFile.Text);
}
else {
throw new FileNotFoundException();
}
}

catch (FileNotFoundException ex) {
lblStatus.Text += ex.Message;
}
catch (Exception ex) {
lblStatus.Text += ex.Message;
}


}

File.delete

Ganesan BalaKrishnan replied to Santhosh N on 06-Aug-09 09:21 AM

no i need to delete file like below

File.Delete("http:\\test\test1\test.pdf")

There are couple of key points to be realized before you attempt such a code.

[)ia6l0 iii replied to Ganesan BalaKrishnan on 06-Aug-09 09:26 AM
To delete a specific file, you need to ensure that the account under which the ASP.Net engine runs needs sufficient priveleges to delete the file. 

And also since the Delete method expects a physical path, you need to use Server.Mappath to retrieve the physical path of the file on the webserver. along with the Path.GetFileName
so you need to do like, 

string physicalPath = Server.Mappath (Path.GetFileName(url));
Re
Ravenet Rasaiyah replied to Ganesan BalaKrishnan on 06-Aug-09 10:22 AM
Hi

When you are try to delete relative ur file you need find the server file path to delete.

NOte: you  need full right to delete that file, or else  you will get access denied error.

Firs get the name from then url

string fileName=Path.GetFileName(url);

string fullPath=Server.MapPath(fileName);

File.Delete(fullPath);


Thank you
http://www.codegain.com