473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check File Exists in Web Share (HTTP)

Manikgisl
37 New Member
HI.

How to check File exists in Web Share C#
Expand|Select|Wrap|Line Numbers
  1. try
  2. {
  3.     WebRequest request = HttpWebRequest.Create("http://www.microsoft.com/NonExistantFile.aspx");
  4.     request.Method = "HEAD"; // Just get the document headers, not the data.
  5.     request.Credentials = System.Net.CredentialCache.DefaultCredentials;
  6.     // This may throw a WebException:
  7.     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  8.     {
  9.         if (response.StatusCode == HttpStatusCode.OK)
  10.         {
  11.             // If no exception was thrown until now, the file exists and we 
  12.             // are allowed to read it. 
  13.             MessageBox.Show("The file exists!");
  14.         }
  15.         else
  16.         {
  17.             // Some other HTTP response - probably not good.
  18.             // Check its StatusCode and handle it.
  19.         }
  20.     }
  21. }
  22. catch (WebException ex)
  23. {
  24.     // Cast the WebResponse so we can check the StatusCode property
  25.     HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
  26.  
  27.     // Determine the cause of the exception, was it 404?
  28.     if (webResponse.StatusCode == HttpStatusCode.NotFound)
  29.     {
  30.         MessageBox.Show("The file does not exist!");
  31.     }
  32.     else
  33.     {
  34.         // Handle differently...
  35.         MessageBox.Show(ex.Message);
  36.     }
  37. }
  38.  

i try this above but excpection is thrown while excuting the following Code

using (HttpWebRespons e response = (HttpWebRespons e)request.GetRe sponse())

please Help me

Thanks in Advance

With Regards,
Mani
Jan 6 '09 #1
2 9802
Tony K
1 New Member
I know this post is really old, but I experienced a similar problem and I thought it would be worth sharing my experience.

The above code threw an exception in the same place when I tried to test for the existence of a .WSDL document. The error code was a (403) Forbidden. I don't think the error code is important, but what got it to work probably is. When I commented out the request.Method = "HEAD"; no exception was thrown and an existing document was identified.

Based on my experiences, I think it might be fair to say that not all web documents have headers. So, when the above exception is thrown, in some cases, it might be appropriate to test against the whole document, as in the case with .WSDL files.

I hope someone finds this helpful.

-- Tony
Sep 21 '09 #2
Plater
7,872 Recognized Expert Expert
While the HTTP spec says HEAD is a required minimal implementation, I don't believe many servers support it. (Possibly do to some sort of abuse?)
403 forbidden is what normally triggers the popup box in your browser requesting a username/password. It's possible that the server responds with a 403 on HEAD requests, purely for the administrator to do developement work with it.

I don't know how this question slipped by me back in january. The line in question will throw an exception on 404 (file not found) errors, and proabably all 4xx and 5xx statuscodes (I think there is even a setting to make 3xx statuscodes trigger an error). Which confuses me as to what the OPs problem was. They have code in the catch block to determine the status code.
Sep 21 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2967
by: DCK | last post by:
Hello I've path to file, which look like this: \\COMPUTER\D$\C++\FILE_TO_DELETE.JPG This path was generated by os.path.walk() function. When i try to delete this file, os.remove() can't find it, os.path.fileexists() can't find it :( I can delete other files (i.e. \\COMPUTER\D$\C\FILE_TO_DELETE.JPG). I guess there's a problem with this C++...
4
4680
by: Rene' Nielsen | last post by:
Context: Running Windows 2003 Server on an intranet. A web is configured with an anonymous access account that is a domain account that has been granted the desired access to a file on another server in the domain. An asp page in the web attempts to open (or any other access) the remote file and fails. Auditing is enabled on the...
6
8734
by: Jennifer Smith | last post by:
Currently we have a site that allows users to listen to mp3 files. It is creating bandwidth issues. So we want to move the mp3 files to an ISP that caps bandwidth usage. Ours is currently burstable. Now I am able to do the following: Set fs = CreateObject("Scripting.FileSystemObject") If fs.fileExists(music_file) Then 'show music file...
1
3775
by: Ian | last post by:
I have an application called "Test.exe" and this application has a config file called "Test.exe.config" and it holds the connection string for the database calls in there. To get the conectionstring out of the config file the following lines of code are utilized. Private settings As New System.Configuration.AppSettingsReader Private o As...
7
33458
by: andylcx | last post by:
Hi all: The c++ language can check whether the file exist or not. I am wondering how c language does this job? Thanks a lot! Andy
6
13703
by: Chad Crowder | last post by:
Getting the following error on my production server whether the file exists or not: "System.IO.IOException: Cannot create a file when that file already exists." Here's the code generating the error (seems to be happening when I try creating a directory) If dirmgr.Exists("s:\blah\" & txt_name.Text) Then lblerror.Text = lblerror.Text &...
26
2435
by: ValK | last post by:
Hi, Here is my problem. I need to check if scanner finished scanning the document. I'm using scanner interface that came with the scanner. I tried to open file and catch IO Exception for "The process cannot access the file ....., because it is being used by another process." It worked with .PDF but not with TIF files. It will allow me to...
0
1182
by: =?Utf-8?B?S2l0dHlIYXdr?= | last post by:
I am trying to use the File.Exists method on image files that are located in a virtual directory on a UNC share from IIS6. The UNC share is located on a Windows 2000 Server machine. I can view the files just file over HTTP on the IIS server. However, when I use File.Exists it always returns False. Of course I am using server.mappath with...
8
2135
by: Alan | last post by:
Hello, I need to write a systemtray program to check every minute if a certain intranet page exists. Would "webBrowser.Navigate" and catching exceptions to it be a good choice in terms of load on the PC? If the page exists, the program should load and display it. The program would run on a couple of thousand PCs and the idea is to have a...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.