- Home /
Gain access to directory
Hey guys, I am trying to write a code that downloads "extra" content to the users computer after the application has been downloaded (to reduce download time and save space). So far I have only researched it, and attempted to try with a copy-and-pasted bit of code. I am getting an "UnauthorizedAccessException", so I asume that I can't download a file from the internet without the users permission; and thats why I am writing this question.
My code (mainly from http://www.csharp-examples.net/download-files) as well as the error I am currently getting are at the bottom, respectively.
using System;
using System.IO;
using System.Net;
using UnityEngine;
using System.Security.AccessControl;
//Written by Gibson Bethke
public class Download : MonoBehaviour {
static public string newFile = "/Users/" + Environment.UserName + "/Desktop/Downloads";
void Start () {
print(newFile);
if (Directory.Exists(newFile)) {
} else {
Directory.CreateDirectory (newFile);
}
}
void OnGUI () {
if (GUI.Button(new Rect(1300,875,100,50), "Download")) {
WebClient webClient = new WebClient();
webClient.DownloadFile("http://db.tt/qXWBNu3g", newFile);
}
}
}
UnauthorizedAccessException: Access to the path '/Users/************/Desktop/Downloads' is denied.
System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options)
System.IO.FileStream..ctor (System.String path, FileMode mode)
(wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode)
System.Net.WebClient.DownloadFileCore (System.Uri address, System.String fileName, System.Object userToken)
System.Net.WebClient.DownloadFile (System.Uri address, System.String fileName)
Rethrow as WebException: An error occurred performing a WebClient request.
System.Net.WebClient.DownloadFile (System.Uri address, System.String fileName)
System.Net.WebClient.DownloadFile (System.String address, System.String fileName)
(wrapper remoting-invoke-with-check) System.Net.WebClient:DownloadFile (string,string)
Download.OnGUI () (at Assets/Download From Web/Download.cs:22)
So, if anyone has any advice, I will really appreciate if you share it with us/me.
Thanks for all your help! Gibson
P.S. I am aware that the file I am currently trying to download is not "extra" content, but a hand-drawn image of a pony. P.P.S. I am not trying to do this in/with a web player; I am trying it in the engine itself, and I intend to use it in a standalone.
Answer by Rod-Green · Dec 26, 2011 at 10:04 PM
I suspect you are running into basic unity player permission issues. You can't download to a user dir through a web build of the game. Which is what I assuming you're trying to do here. You can download a package and use it directly but there's no way to save it locally via webbuild/deployed builds.
Hey Rod Green, unfortunately I am not trying to do this with the web player, I am just trying it in the engine itself, and I plan on using it in a standalone. Thanks for your prompt reply, though... I probably should have mentioned that it was for a standalone in my original question.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how can i use ZipArchive class ,How can i use .net ZipArchive Class 0 Answers
Printing in Unity 1 Answer
custom startup resolution window 1 Answer