0

I have downloaded an image with the following code:

bool pageExists = false;
// Check if webpage exists
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://image.tmdb.org/t/p/w780" + imagePath);
request.Method = WebRequestMethods.Http.Head;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
      pageExists = response.StatusCode == HttpStatusCode.OK;
}

// Download image
if (pageExists)
{
   string localFilename = @"C:\Users\Giri\Desktop\giri" + id + ".jpg";
   using (WebClient client = new WebClient())
   {
       client.DownloadFile("http://image.tmdb.org/t/p/w780" + imagePath, localFilename);
   }
}

For now, I have just been saving this image on my Desktop.

My question is how do I go about storing this image in my WPF application programmatically within a resources folder or a folder I have generated myself? The images should persist in that the next time the application is run, the added images should remain.

Is there an accepted place I should be storing my images?

Thanks for your help.

  • How about getting the `Environment.CurrentDirectory` and adding a `Resource` folder to that path and saving the images there. – Piyush Parashar Nov 29 '14 at 13:51
  • Just a note: by using a HttpWebRequest *and* a WebClient you're downloading the image twice. You should use only one of them, preferably the WebClient, because it is simpler. – Clemens Nov 29 '14 at 15:13
  • Thanks @PiyushParashar. I am currently doing it the way you have suggested. I was wondering, when this application is packaged up and being used by a consumer, will `Environment.CurrentDirectory` still point to the same place? Thank you for your help. –  Nov 30 '14 at 01:56

1 Answers1

0

Please use AppDomain.CurrentDomain.BaseDirectory. Which will give you the directory of your executable. Even in deployed code this should give you the right value. But other values like Environment.CurrentDirectory can give different value based on from where you are calling it etc.

See this question Best way to get application folder path

Community
  • 1
  • 1
Piyush Parashar
  • 866
  • 8
  • 20