Click here to Skip to main content
15,849,907 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a folder with name 'reports' and I would like to delete one of the files present in it dynamically..when I am trying to do this

it's throwing an exception like..

"Illegal characters in path"

I think I am giving the right path?

my code written in code behind

<br />
 File.Delete(Server.MapPath("~reports") + "\\" + ASPxDropDownEdit1.SelectedItem.Text + "*.pdf");<br />

any help?

thank u !!
A Pradeep
Updated 28-Jul-11 11:53am
Amund Gjersøe 28-Jul-11 16:46pm    
You say the files is within the reports folder, so I assume ASPxDropDownEdit1.SelectedItem.Text is the file name. Could it be the '*' that is the illegal character?
Orcun Iyigun 28-Jul-11 17:02pm    
Yes I am suspicious about the value there as well. The OP should debug it and if the value has a illegal character or not.

File.Delete does not accept wild cards, so the * is an invalid character.

You could try iterating over all the files that match the search pattern like this:

DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~reports"));

foreach (FileInfo file in 

Hope this helps,
Share this answer
AnuguPradeep 28-Jul-11 17:09pm    
thank u !!
Fredrik Bornander 28-Jul-11 17:13pm    
Glad I could help.
Please mark the the answer as Accepted if it solved your problem so that we can close the question.
Orcun Iyigun 28-Jul-11 18:10pm    
Good catch my 5! I didnt see the "*" there.
walterhevedeich 28-Jul-11 20:40pm    
Good spot.
Try the following and let me know your feedback.

string FileToDelete;
FileToDelete=ASPxDropDownEdit1.SelectedItem.Text.Trim().ToString() + ".pdf";
// Set full path to file
FileToDelete = Server.MapPath("~/reports/"+FileToDelete+");
// Delete a file
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900