8

I am trying to work with WebClient but it is giving me errors so I check in several forums (included this one) and they where telling to put

In the top of the file:

using System.Net 

And after where I want use the WebClient:

 WebClient webClient = new WebClient();
 webClient.DownloadFile ("http://mysite.com/myfile.txt", @"c:\myfile.txt");

And I get this error:

The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?)

In the other forums the code that I just wrote above seems to be the solution but is not working for me.

Iban Arriola
  • 1,946
  • 6
  • 30
  • 80

1 Answers1

19

Given your tags, it sounds like you may be building a Windows Store app - in which case you need to use HttpClient instead of WebClient.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • what if my class library already using webClient and i want to use it on Windows Store App – Mike Darwish May 16 '17 at 09:04
  • 1
    @MikeDarwish: Then you should change it to use `HttpClient` - it's as simple as that. You just *can't* use `WebClient` in Windows Store apps, as far as I'm aware. – Jon Skeet May 16 '17 at 09:23
  • @JonSkeet but my class already using functions on WebClient public class CookieAwareWebClient : WebClient – Mike Darwish May 16 '17 at 09:36
  • @JonSkeet actually that is because i am using class library and i am trying to reference this library from uwp,keep in your mind i am already using this librray on my xamarin.forms.Droid project – Mike Darwish May 16 '17 at 10:00
  • 1
    @MikeDarwish: That doesn't magically make WebClient available in UWP though. Just because you *want* it to be feasible doesn't make it so. – Jon Skeet May 16 '17 at 10:20
  • @JonSkeet .. I understand you, i am trying to use same library on uwp & android projects on Xamarin.forms .thx – Mike Darwish May 16 '17 at 10:29
  • 1
    @MikeDarwish: And I'm saying that you'll need to either change the project to use HttpClient (assuming it's available in the version you're targeting) or use different implementatations. If you package up your library in a NuGet package, you could depend on the same *package* from both places, but with different implementations based on the target platforms. – Jon Skeet May 16 '17 at 10:34