Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: CC#
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");
    }
}
Posted 21-Sep-12 10:00am
beratxt658
Comments
Wes Aday at 21-Sep-12 16:06pm
   
What kind of a directory name is "http://www.berat.net/lisanslar"?
beratxt at 21-Sep-12 16:12pm
   
what kind of meaning ?..it is an example domain and root..
Wes Aday at 21-Sep-12 16:14pm
   
Exactly. It's a URL NOT a directory. Why do you suppose it does not work?
beratxt at 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 ?
Wes Aday at 21-Sep-12 16:23pm
   
For exactly the same reason.
beratxt at 21-Sep-12 16:27pm
   
The reason can be same..But what is the solution for it ? :) how can i check the file whether or not exists on the Server..it works on local...
Wes Aday at 21-Sep-12 16:32pm
   
You would need a web service that can check the local drives on the webserver. Or you would need to check that in your code-behind. Get a list of files from an FTP server that can access the web servers files. Map the web servers drives as network drives. You are not going to be able to query a web servers files remotely like you are trying to do. Not going to happen.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
v2
Comments
beratxt at 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ő at 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 at 21-Sep-12 16:31pm
   
Correct, a 5. OP just wanted you to say that again. :-) --SA
beratxt at 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ő at 21-Sep-12 16:37pm
   
This is why we are here.
Wes Aday at 21-Sep-12 17:21pm
   
Might be why you are here.... I'm here for the glory :-)
Sergey Alexandrovich Kryukov at 21-Sep-12 17:57pm
   
You are already famous. :-) --SA
Wes Aday at 21-Sep-12 17:59pm
   
lol or infamous.
Sergey Alexandrovich Kryukov at 21-Sep-12 18:09pm
   
:-), no! --SA
Wonde Tadesse at 21-Sep-12 18:31pm
   
5+
beratxt at 22-Sep-12 5:02am
   
all of us are here for each other :) thanks
Wonde Tadesse at 22-Sep-12 14:37pm
   
No prob.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use Server.MapPath Instead of that URL
  Permalink  
Comments
beratxt at 21-Sep-12 16:12pm
   
in winform..not asp.net
CoderVivs at 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 at 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 339
1 Sergey Alexandrovich Kryukov 305
2 OriginalGriff 290
3 Peter Leow 171
4 Guruprasad.K.Basavaraju 170
0 Sergey Alexandrovich Kryukov 8,979
1 OriginalGriff 5,280
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid