IO.File.Exists()始终返回false

问题描述:

考虑的button1_Click下面的代码IO.File.Exists()始终返回false

Dim stFile as String = IO.Path.Combine(Server.MapPath("~/"), "uploads/text/file1.txt") 
If IO.File.Exists(stFile) Then 
    ' Do some processing 
End If 

已存在总是在上面的代码块

返回false这里是Button2_Click代码块

Dim stFile as String = IO.Path.Combine(Server.MapPath("~/"), "uploads/text/file1.txt") 
Response.Clear() 
Response.ContentType = "text/plain" 
Response.AppendHeader("content-disposition", "attachment;filename=abc.txt") 
Response.TransmitFile(stFile) 
Response.Flush() 
End If 

这总是下载相同的文件。可能是什么问题呢?

+1

“stFile”的值是什么,使用调试er –

+0

如果uploads文件夹位于您项目的根目录中,那么只需使用以下命令:Dim stFile as String = Server.MapPath(“〜/ uploads/text/file1.txt”)' –

前段时间我也对这个问题感到崩溃,发现使用“/”和特殊字符可能会产生这种情况。

Path.Combine总是返回带有“\”的路径。

尝试改变uploads/text/file1.txtuploads\text\file1.txt

如果生成动态文件名的时候,尽量避免包括可能需要的URL编码如%([space]

一些概念的任何特殊字符在这篇文章中可能看起来不合逻辑,但是使用\,/和特殊字符的组合浪费了我的大约8-10小时