how can I check if web folder exists?

check for existing folder before save file to folder.

Page 1 of 1

5 Replies - 5470 Views - Last Post: 27 June 2009 - 06:21 PM Rate Topic: -----

#1 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 07-May 08

how can I check if web folder exists?

Post icon  Posted 14 June 2009 - 01:53 PM

Hi,

I'm able to upload file to specific existing web folder if I know beforehand, but what if I don't know which web folder exists before uploading file?

To upload file, I use WebClient.

can anyone help me on that?
Is This A Good Question/Topic? 0
  • +

Replies To: how can I check if web folder exists?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: how can I check if web folder exists?

Posted 14 June 2009 - 02:19 PM

Well if you specify a folder that doesn't exist and then you attempt to OpenWrite or OpenRead to it, it throws you the WebException because the URI would be invalid. You can catch this error and report to the user that the folder doesn't exist.

:)
Was This Post Helpful? 0
  • +
  • -

#3 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 07-May 08

Re: how can I check if web folder exists?

Posted 14 June 2009 - 02:47 PM

You are right. I will try that out. thanks.
Was This Post Helpful? 0
  • +
  • -

#4 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 07-May 08

Re: how can I check if web folder exists?

Posted 14 June 2009 - 03:24 PM

I use WebClient.UploadFile() to upload file, it returns byte type data that has error code 409 conflict and I have no idea what that is. So instead of calling UploadFile, I'm using DownloadFile instead and create dummy file on each existing folder, then download dummy file from each existing folder. If success downloading then folder exists else doesn't exist. It seems to work fine, but creating extra file on web folder not sure if it's good or not.

thanks.
Was This Post Helpful? 0
  • +
  • -

#5 searockruz  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 460
  • Joined: 07-March 09

Re: how can I check if web folder exists?

Posted 15 June 2009 - 02:45 AM

its totally a bad idea

have you tried using, WebRequest and WebResponse
Was This Post Helpful? 1
  • +
  • -

#6 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 07-May 08

Re: how can I check if web folder exists?

Posted 27 June 2009 - 06:21 PM

Following your suggestion then did some researching online then use WebRequest and HttpWebResponse. Below is code block.

	   public void CheckWebFoldersExist()
		{
			try
			{
				WebClient client = new WebClient();
				client.Credentials = CredentialCache.DefaultCredentials;

				// Create a request for the URL.		 
				WebRequest request = WebRequest.Create("myAddress");
				
				// If required by the server, set the credentials.
				request.Credentials = CredentialCache.DefaultCredentials;
				
				// Get the response.
				HttpWebResponse response = (HttpWebResponse)request.GetResponse();

				//check response status
				if (string.Compare(response.StatusDescription, "OK", true) == 0)
				{
					//URL exists so that means folder exists
				}
				else
				{
					//URL does not exist so that means folder does not exist
				}	  

			}
			catch (Exception error)
			{
				//error catching
			}
		}




Thanks searockruz for your tips

This post has been edited by turtleC++: 27 June 2009 - 07:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1