检查服务器上是否存在文件

时间:2021-11-12 13:27:12

I am trying to check if a file is on the server with the C# code behind of my ASP.NET web page. I know the file does exist as I put it on the server in a piece of code before hand. Can anyone see why it is not finding the file. This is the code:

我正在尝试检查一个文件是否在服务器上,而c#代码在我的ASP后面。净的web页面。我知道这个文件确实存在,因为我把它放在服务器上的一段代码中。谁知道为什么它没有找到文件。这是代码:

wordDocName = "~/specifications/" + Convert.ToInt32(ViewState["projectSelected"]) + ".doc";
ViewState["wordDocName"] = wordDocName;
if (File.Exists(wordDocName))
{
    btnDownloadWordDoc.Visible = true;
}
else
{
    btnDownloadWordDoc.Visible = false;
}

8 个解决方案

#1


40  

the file path should be physical not virtual. Use

文件路径应该是物理的而不是虚拟的。使用

if (File.Exists(Server.MapPath(wordDocName)))

#2


2  

File.Exists() and probably everything else you want to do with the file will need a real Path.

file . exist()和您想要对该文件进行的其他操作可能都需要一个真正的路径。

Your wordDocName is a relative URL.

您的wordDocName是一个相对URL。

Simply use

简单地使用

string fileName = Server.MapPath(wordDocName);

#3


1  

Use

使用

Server.MapPath("~/specifications/" + Convert.ToInt32(ViewState["projectSelected"]) + ".doc")

to get the fully-qualified path. That should do the trick for ya.

获取完全限定路径。这对你应该有好处。

#4


1  

You need to use Server.MapPath e.g.

您需要使用服务器。MapPath如。

    wordDocName = Server.MapPath("~/specifications/" + Convert.ToInt32(ViewState["projectSelected"]) + ".doc");
    ViewState["wordDocName"] = wordDocName;
    if (File.Exists(wordDocName))
    {
        btnDownloadWordDoc.Visible = true;
    }
    else
    {
        btnDownloadWordDoc.Visible = false;
    }

#5


0  

The character "~" is a special char in ASP.NET to get virtual path specifications and simply means "root directory of the application". Is is not understood by the .NET BCL like the File API and must be mapped first into a physical path with Server.MapPath() as others stated.

“~”是ASP中的一个特殊字符。NET获取虚拟路径规范,简单地表示“应用程序的根目录”。和文件API一样,. net BCL也不理解Is,必须像其他人所说的那样,首先使用Server.MapPath()将其映射到物理路径中。

#6


0  

You have to convert the path to a physical path with Server.MapPath(relativePath)

您必须使用Server.MapPath(相对论路径)将路径转换为物理路径

if (File.Exists(filePath))

wordDocName = "~/specifications/" + ViewState["projectSelected"] + ".doc";        
btnDownloadWordDoc.Visible = File.Exists(Server.MapPath(wordDocName));

#7


0  

this might not work if the directory holding the file is referenced by a junction/symbolic link. I have this case in my own application and if I put the REAL path to the file, File.Exists() returns true. But if I use Server.MapPath but the folder is in fact a junction to the folder, it seems to fail. Anyone experienced the same behaviour?

如果包含该文件的目录被连接/符号链接引用,这可能无法工作。在我自己的应用程序中有这种情况,如果我将实际路径放入文件,file . exist()返回true。但是如果我用服务器的话。但是文件夹实际上是文件夹的一个结,它似乎失败了。有人有过同样的经历吗?

#8


-3  

string docname="traintatkalantnoy.txt";

字符串docname =“traintatkalantnoy.txt”;

string a = (Server.MapPath(docname)); if (File.Exists(a))

字符串a =(Server.MapPath(docname));如果(File.Exists(a))

#1


40  

the file path should be physical not virtual. Use

文件路径应该是物理的而不是虚拟的。使用

if (File.Exists(Server.MapPath(wordDocName)))

#2


2  

File.Exists() and probably everything else you want to do with the file will need a real Path.

file . exist()和您想要对该文件进行的其他操作可能都需要一个真正的路径。

Your wordDocName is a relative URL.

您的wordDocName是一个相对URL。

Simply use

简单地使用

string fileName = Server.MapPath(wordDocName);

#3


1  

Use

使用

Server.MapPath("~/specifications/" + Convert.ToInt32(ViewState["projectSelected"]) + ".doc")

to get the fully-qualified path. That should do the trick for ya.

获取完全限定路径。这对你应该有好处。

#4


1  

You need to use Server.MapPath e.g.

您需要使用服务器。MapPath如。

    wordDocName = Server.MapPath("~/specifications/" + Convert.ToInt32(ViewState["projectSelected"]) + ".doc");
    ViewState["wordDocName"] = wordDocName;
    if (File.Exists(wordDocName))
    {
        btnDownloadWordDoc.Visible = true;
    }
    else
    {
        btnDownloadWordDoc.Visible = false;
    }

#5


0  

The character "~" is a special char in ASP.NET to get virtual path specifications and simply means "root directory of the application". Is is not understood by the .NET BCL like the File API and must be mapped first into a physical path with Server.MapPath() as others stated.

“~”是ASP中的一个特殊字符。NET获取虚拟路径规范,简单地表示“应用程序的根目录”。和文件API一样,. net BCL也不理解Is,必须像其他人所说的那样,首先使用Server.MapPath()将其映射到物理路径中。

#6


0  

You have to convert the path to a physical path with Server.MapPath(relativePath)

您必须使用Server.MapPath(相对论路径)将路径转换为物理路径

if (File.Exists(filePath))

wordDocName = "~/specifications/" + ViewState["projectSelected"] + ".doc";        
btnDownloadWordDoc.Visible = File.Exists(Server.MapPath(wordDocName));

#7


0  

this might not work if the directory holding the file is referenced by a junction/symbolic link. I have this case in my own application and if I put the REAL path to the file, File.Exists() returns true. But if I use Server.MapPath but the folder is in fact a junction to the folder, it seems to fail. Anyone experienced the same behaviour?

如果包含该文件的目录被连接/符号链接引用,这可能无法工作。在我自己的应用程序中有这种情况,如果我将实际路径放入文件,file . exist()返回true。但是如果我用服务器的话。但是文件夹实际上是文件夹的一个结,它似乎失败了。有人有过同样的经历吗?

#8


-3  

string docname="traintatkalantnoy.txt";

字符串docname =“traintatkalantnoy.txt”;

string a = (Server.MapPath(docname)); if (File.Exists(a))

字符串a =(Server.MapPath(docname));如果(File.Exists(a))