简体   繁体   English

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

[英]How to check if a file exists on the web server

I have a file stored on the server and the web page I am populating depends on the fatc that the file exists or not. 我有一个文件存储在服务器上,正在填充的网页取决于文件是否存在的Fatc。

How do I test if the file is available on the server? 如何测试文件在服务器上是否可用? The file comes on the web page as: http://main.server.com/PGT/Reports/ObjectsReport.xml 该文件在网页上显示为: http : //main.server.com/PGT/Reports/ObjectsReport.xml

I have to test the existence of this file and if it is available I will display a link otherwise I want to hide the link. 我必须测试此文件是否存在,如果可用,我将显示一个链接,否则我想隐藏该链接。

The actual path to the server is //main.server.com/inetpub/wwwroot/PGT/Reports/ObjectsReport.xml but I don't have access to the server (and therefore to the file) on the network. 服务器的实际路径是//main.server.com/inetpub/wwwroot/PGT/Reports/ObjectsReport.xml,但是我无法访问网络上的服务器(因此也无法访问文件)。 I can only access it using the web page. 我只能使用网页访问它。 Is there a way to test that the server has the file or not display the link? 有没有一种方法可以测试服务器是否具有文件或不显示链接? (hlObjectsReport.Visible = false;) (hlObjectsReport.Visible = false;)

I have tried to use the following: 我尝试使用以下方法:

Uri validatedUri; Uri validatedUri; Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out validatedUri); Uri.TryCreate(uri,UriKind.RelativeOrAbsolute,out validatedUri);

But it returns a valid address even if the file is not there. 但是,即使文件不存在,它也会返回有效地址。

Thanks Tony. 谢谢托尼。

use System.IO.File.Exists() ( Documentation ) 使用System.IO.File.Exists()( 文档

if(System.IO.File.Exists([path goes here]))
{
  // do something
}

If you're not sure of the physical path, you can substitute the following for [path goes here] above: 如果您不确定物理路径,可以将以下内容替换为上面的[path go here]:

Server.MapPath(/PGT/Reports/ObjectsReport.xml) Server.MapPath(/PGT/Reports/ObjectsReport.xml)

( Documentation ) 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM