在文件路径中抛出错误,指出非法字符 [英] throwing error in file path saying illegal characters

查看:116
本文介绍了在文件路径中抛出错误,指出非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"reports"的文件夹,我想动态删除其中的一个文件.
会引发类似..
的异常
路径中的非法字符"

我认为我给的路正确了吗?

我的代码写在后面的代码中

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 />



有帮助吗?

谢谢你!!
Pradeep



any help?

thank u !!
A Pradeep

推荐答案

File.Delete不接受通配符,因此 * 是无效字符.

您可以尝试遍历所有与搜索模式匹配的文件,如下所示:

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 
         directory.GetFiles(String.Format("{0}*.pdf", 
         ASPxDropDownEdit1.SelectedItem.Text)))
{
    file.Delete();
}




希望这会有所帮助,
弗雷德里克(Fredrik)




Hope this helps,
Fredrik


尝试以下操作,让我知道您的反馈意见.

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
File.Delete(FileToDelete);


这篇关于在文件路径中抛出错误,指出非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆