我试图检查服务器上是否存在文件,如果文件存在则警告使用,以便他可以重命名文件并再次上传.但我的代码总是返回true,即使我添加!到System.IO.File.Exists
此代码适用于ASP.net webform应用程序
string filename = Path.GetFileName(FileUploadControl.FileName);
if (System.IO.File.Exists("../pdf/news/" + FileUploadControl.FileName))
{
ViewState["_fileName"] = null;
StatusLabel.Text = "File with this name already exsists, Please rename file and Upload gain";
}
else
{
FileUploadControl.SaveAs(Server.MapPath("../pdf/news/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
//_fileName = FileUploadControl.FileName;
ViewState["_fileName"] = FileUploadControl.FileName;
}
Run Code Online (Sandbox Code Playgroud)
我确信我做错了但却无法弄清楚是什么.
这里最可能的问题是您Server.MapPath在存在检查之前没有使用.在哪里看?我们不知道.但是因为你打算以后这样做:早点移动它:
var path = Path.Combine(Server.MapPath("../pdf/news/"), filename);
Run Code Online (Sandbox Code Playgroud)
并使用path了这两种的存在检查和最后的创作.
请注意,编辑Web应用程序树中的文件是个坏主意:
| 归档时间: |
|
| 查看次数: |
1261 次 |
| 最近记录: |