0

I coded an app and I am trying to download a apk from my webspace now. I googled the whole day now and everything I found was for Java I think, but I need C#.

This is what I found (not only this but it's an example): Downloading APK from server and installing it to device (but it's not working I don't know why).

But I have some problems with the code. I can't use the setRequestMethod or getinputstream etc.

Then I tried out this because it seemed to be much easier:

WebClient webClient = new WebClient();
Uri newUri = new Uri("http://mywebspace.bplaced.net/AppDownload.apk");
webClient.DownloadFile(newUri,Path.Combine(Android.OS.Environment.DirectoryDownloads, "AppDownload.apk"));

This is also not working. I'm getting the message "parts of the path not found" (kinda like this, my Visual Studio isn't in English). Doesn't matter what I enter as my path.

Christian Junk
  • 1,000
  • 1
  • 8
  • 22
Ray Yago
  • 115
  • 1
  • 1
  • 12
  • Possible duplicate of [How to download a file from a URL in C#?](https://stackoverflow.com/questions/307688/how-to-download-a-file-from-a-url-in-c) – Christian Junk Jan 06 '18 at 21:57
  • When asking questions here, please keep them succinct and free of begging and pleading. Thank you. – halfer Jan 06 '18 at 22:38
  • Next time, please provide more information on your environment. E.g. tell us that you are coding a mobile app for Android using Xamarin. – Christian Junk Jan 06 '18 at 22:49

1 Answers1

1

This seems to be a duplicate question, which already has been answered here: How to download a file from a URL in C#?

Nevertheless, I have tested your code and for me it is working. I am able to download a file from an url. So, my guess is, that you have a typo in your URL, a problem with the firewall, etc.

Please copy the URL you have entered in the source code and paste it to a Browser of your choice. And see if the download starts. Just to be sure that the URL is correct.

It would better to provide us with the whole error message.

Christian Junk
  • 1,000
  • 1
  • 8
  • 22
  • Oh i dont saw that post. But the URL is working but when i try it trhough my android App its not working anymore. i get every time this message: System.IO.DirectoryNotFoundException: Could not find a part of the path "/Download/AppDownload.apk". Do i have to do something special with my path when im working with android? i mean if i get the message a part of the path wasnt found , maybe i have to change something. like on computer, u use something like "C:" but on the phone i just used "downloads/app.apk". hard to explain for me – Ray Yago Jan 06 '18 at 22:34
  • Ok. This makes things a little bit clearer. It seems not to be a problem with the download but, according to the error message, a problem with storing the download. Please try to replace your `Path.Combine` with `Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "AppDownload.apk")`. Also double check if your app requested the correct permissions to be able to write to the required directories. – Christian Junk Jan 06 '18 at 22:40
  • Thanks. I think im getting closer. But now i get the message: Access to the path "/storage/emulated/0/AppDownload.apk" is denied. How can i give the app the required permissions? And i have one little question if its okey :3. ExternalStorageDirectory‌ means taht i need a memory card for that? i have one but just to be sure if its 100% required :D – Ray Yago Jan 06 '18 at 22:49
  • You will need to update your `AndroidManifest.xml` and add the following two entries: `uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"` and `uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"`. Take a look at the [documentation](https://developer.xamarin.com/recipes/android/general/projects/add_permissions_to_android_manifest/). – Christian Junk Jan 06 '18 at 22:54
  • this is working. but i cant find the file anywhere now. – Ray Yago Jan 06 '18 at 23:14
  • Ok. Now, please try to replace your `Path.Combine` with this code: `Path.Combine(Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads), "AppDownload.apk")`. Sorry for the workarounds, but I am trying to use the method of elimination to help you solving the problem. – Christian Junk Jan 06 '18 at 23:23
  • ITS WORKING <3. That helped me a lot. i just looked at the download directory and there it is, my apk. i dont know why its working cause the path is still the same right? just coded in a other way :D. – Ray Yago Jan 07 '18 at 09:59