Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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
C#
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");
    }
}
Posted
Comments
[no name] 21-Sep-12 16:06pm    
What kind of a directory name is "http://www.berat.net/lisanslar"?
beratxt 21-Sep-12 16:12pm    
what kind of meaning ?..it is an example domain and root..
[no name] 21-Sep-12 16:14pm    
Exactly. It's a URL NOT a directory. Why do you suppose it does not work?
beratxt 21-Sep-12 16:21pm    
yes i know...can u look the code below ?


FileInfo f = new FileInfo(@"http://www.ebitech.net/lisanslar/berat.txt");

if (f.Exists==true)
{
MessageBox.Show("yes exists");
}
else
{
MessageBox.Show("non exist");
}
this code also doesnt work why ?
[no name] 21-Sep-12 16:23pm    
For exactly the same reason.

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[^]
 
Share this answer
 
v2
Comments
beratxt 21-Sep-12 16:23pm    
Hi Mr.Zorgo ,

I dont want to list the files existing on the server.I just wana check if the file exists in my website..The code is like below.Above one is not true ..


yes i know...can u look the code below ?


FileInfo f = new FileInfo(@"http://www.ebitech.net/lisanslar/berat.txt");

if (f.Exists==true)
{
MessageBox.Show("yes exists");
}
else
{
MessageBox.Show("non exist");
}
this code also doesnt work why ?
Zoltán Zörgő 21-Sep-12 16:28pm    
It is not working, because this is not meant to work via http, it is designed to work on a file system. Please look at the second link I have posted. Be aware that what you try to check might not be a file - might be, but if it is on a web server it is a web resource, and you have to treat it as such.
Here are also two implementations: http://www.dotnetthoughts.net/how-to-check-remote-file-exists-using-c/
Sergey Alexandrovich Kryukov 21-Sep-12 16:31pm    
Correct, a 5. OP just wanted you to say that again. :-)
--SA
beratxt 21-Sep-12 16:33pm    
Thanks friends..Sorry for my knowledge!..but i was thingking it works also for http:(
sorry for making u disturb..thanks for your help!
Zoltán Zörgő 21-Sep-12 16:37pm    
This is why we are here.
Use Server.MapPath Instead of that URL
 
Share this answer
 
Comments
beratxt 21-Sep-12 16:12pm    
in winform..not asp.net
CoderVivs 21-Sep-12 16:19pm    
have u checked ur FileEntries list ? is it cntains the Filename "berat.txt" if yes then check it the case sensivity
beratxt 21-Sep-12 16:26pm    
yes i tried.also i tried the code below..can u give an idea ? wrong what ?


Hi Mr.Zorgo ,

I dont want to list the files existing on the server.I just wana check if the file exists in my website..The code is like below.Above one is not true ..


yes i know...can u look the code below ?


FileInfo f = new FileInfo(@"http://www.ebitech.net/lisanslar/berat.txt");

if (f.Exists==true)
{
MessageBox.Show("yes exists");
}
else
{
MessageBox.Show("non exist");
}
this code also doesnt work why ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900