如果file.exists()在网站上不起作用? [英] if file.exists() doesnt work on a website ?

查看:155
本文介绍了如果file.exists()在网站上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试检查目录中是否存在任何文件.

如果berat.txt存在,我将显示YES,如果不是,则显示

下面的代码不起作用.我也尝试了Fileİnfo类和FileExists()方法.权限?

请提前通知我

Hi guys,

I''m triying to check whether or not any file exists in my directory.

İf the berat.txt exists , i will show message YES, if not NO ,

The code below doesnt work.Also i tried Fileİnfo class and FileExists() method..but always false why ? permissions?

please inform me thanks for in advance

string targetDirectory = @"http://www.berat.net/lisanslar";
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
{
    if (fileName=="berat.txt")
    {
        MessageBox.Show("var");
    }
    else
    {
        MessageBox.Show("yok");
    }
}

推荐答案

这不起作用.没有通用的http包装器.您为什么认为可以在Web服务器上列出文件?它们甚至可能根本不存在,路由可以为您提供url中的任何目录结构,这绝对不是真实的,只能生成.仅当您尝试访问的站点在该虚拟文件夹上提供WebDav(基于Web的分布式创作和版本控制)时,该功能才起作用.它是对HTTP协议的一组扩展,该协议允许用户协作编辑和管理远程Web服务器上的文件.因此,如果您对该协议有足够的了解,则可以通过System.Net中的类进行下载工作.您可以在此处找到一个很好的起点: http://admiral-announce.blogspot .hu/2010/04/listing-directory-using-webdav.html [资源.如果您获得200 OK,那么您就拥有了.您不需要下载文件本身,可以使用HEAD方法仅获取文件的http标头.在此处查看答案: http://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid [
This won''t work. There is no general http wrapper for that. Why do you think, that you can list the files on a web server? They might even not exist at all, routing can present you any directory structure in the urls, that is absolutely not real, only generated. Could only work if the site you try to access is serving WebDav (Web-based Distributed Authoring and Versioning) on that virtual folder. It is a set of extensions to the HTTP protocol which allows users to collaboratively edit and manage files on remote web servers. Thus, if you have a good knowledge of this protocol, you can do the downloading job through the classes in System.Net. You can find a good starting point here: http://admiral-announce.blogspot.hu/2010/04/listing-directory-using-webdav.html[^]
But if there is no WebDav on the server, you have to try to fetch or at least test for that file as url. Try to access the @"http://www.berat.net/lisanslar/berat.txt" resource. If you get 200 OK, than you have it. You don''t need to download the file itself, you can fetch only its''s http headers with the HEAD method. See the answers here: http://stackoverflow.com/questions/924679/c-sharp-how-can-i-check-if-a-url-exists-is-valid[^]


使用Server.MapPath代替该URL
Use Server.MapPath Instead of that URL


这篇关于如果file.exists()在网站上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆