I stored a file path in database table like this ../Document/5292013/cal.png
. Now I want to check whether or not the file exists in the server folder. I am using the below code to check this, but it's not working for me.
我在这样的数据库表中存储了一个文件路径../Document/5292013/cal.png。现在我想检查文件是否存在于服务器文件夹中。我使用下面的代码检查这个,但它不适合我。
if (File.Exists(Server.MapPath(root.GetElementsByTagName("FLD_DOC_ID")[0].InnerText)))
{
proof.HRef = Server.MapPath(root.GetElementsByTagName("FLD_DOC_ID")[0].InnerText);
}
Now I check using watch File.Exists(Server.MapPath("Document")) //Returns false
, but server having the same folder.
现在我检查使用watch File.Exists(Server.MapPath(“Document”))//返回false,但服务器具有相同的文件夹。
Please help me to solve this.
请帮我解决这个问题。
3 个解决方案
#1
4
You need to transform the file name to a virtual form before using MapPath
. You must know the specifics of how it needs to be done. For example:
在使用MapPath之前,需要将文件名转换为虚拟表单。您必须知道需要如何完成的具体细节。例如:
string fileName = root.GetElementsByTagName("FLD_DOC_ID")[0].InnerText;
fileName = fileName.Replace("..", "~");
if (File.Exists(Server.MapPath(fileName))
{
// you probably do not want MapPath here:
//proof.HRef = Server.MapPath(root.GetElementsByTagName("FLD_DOC_ID")[0].InnerText);
proof.HRef = System.Web.VirtualPathUtility.ToAbsolute(fileName);
}
#2
1
Try to print out Server.MapPath(root.GetElementsByTagName("FLD_DOC_ID")[0].InnerText) it might be pointing a wrong path or something
尝试打印出Server.MapPath(root.GetElementsByTagName(“FLD_DOC_ID”)[0] .InnerText)它可能指向错误的路径或某事
Any way, checking a file if it exists or not is very trivial:
无论如何,检查文件是否存在都非常简单:
if(File.Exists(the file path))
{
}
#3
0
First you have to get filepath (filename) from database using select query then use that path with file.exists.
首先,您必须使用select query从数据库获取filepath(filename),然后将该路径与file.exists一起使用。
Example:
First get filename or filepath from database then,
首先从数据库获取文件名或文件路径,然后,
if you get only filename then use below code:
如果你只获得文件名,那么使用下面的代码:
if(File.Exits(Server.MapPath("Document/5292013/"+filename)))
{
}
or if you get only filepath then use below code:
或者如果你只获得filepath,那么使用下面的代码:
if(File.Exits(Server.MapPath("filename")))
{
}
Thanks
#1
4
You need to transform the file name to a virtual form before using MapPath
. You must know the specifics of how it needs to be done. For example:
在使用MapPath之前,需要将文件名转换为虚拟表单。您必须知道需要如何完成的具体细节。例如:
string fileName = root.GetElementsByTagName("FLD_DOC_ID")[0].InnerText;
fileName = fileName.Replace("..", "~");
if (File.Exists(Server.MapPath(fileName))
{
// you probably do not want MapPath here:
//proof.HRef = Server.MapPath(root.GetElementsByTagName("FLD_DOC_ID")[0].InnerText);
proof.HRef = System.Web.VirtualPathUtility.ToAbsolute(fileName);
}
#2
1
Try to print out Server.MapPath(root.GetElementsByTagName("FLD_DOC_ID")[0].InnerText) it might be pointing a wrong path or something
尝试打印出Server.MapPath(root.GetElementsByTagName(“FLD_DOC_ID”)[0] .InnerText)它可能指向错误的路径或某事
Any way, checking a file if it exists or not is very trivial:
无论如何,检查文件是否存在都非常简单:
if(File.Exists(the file path))
{
}
#3
0
First you have to get filepath (filename) from database using select query then use that path with file.exists.
首先,您必须使用select query从数据库获取filepath(filename),然后将该路径与file.exists一起使用。
Example:
First get filename or filepath from database then,
首先从数据库获取文件名或文件路径,然后,
if you get only filename then use below code:
如果你只获得文件名,那么使用下面的代码:
if(File.Exits(Server.MapPath("Document/5292013/"+filename)))
{
}
or if you get only filepath then use below code:
或者如果你只获得filepath,那么使用下面的代码:
if(File.Exits(Server.MapPath("filename")))
{
}
Thanks