- Home /
Saving File in new directory - Unauthorized Access Exception
I am trying to save a text file to a newly created directory. I get the following error:
UnauthorizedAccessException: Access to the path 'C:\Data\FS031109' is denied. System.IO.FileStream..ctor (System.String name, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) System.IO.FileStream..ctor (System.String name, FileMode mode, FileAccess access, FileShare share) ...
I've seen on the forums that iPhone applications can only read/write to certain directories, but I am not trying to work on an iPhone application. It's essential that I can create a new folder with a custom name (i.e. username) and save a text file there. Any suggestions? Which directories can I write to? Where can I create new directories, what's restricted?
Many thanks!
Here's what I'm trying to do. :
var firstnameGUI = "First Name"; var surnameGUI = "Surname"; function OnGUI () {
firstnameGUI = GUI.TextField (Rect (25, 80, 150, 30), firstnameGUI); surnameGUI = GUI.TextField (Rect (25, 115, 150, 30), surnameGUI);
if (GUI.Button (Rect (25, 350, 120, 30), "Save User")) {
//create Username
var date = DateTime.Now.ToString("ddMMyy");
usernameGUI = firstnameGUI.Substring(0,1) + surnameGUI.Substring(0,1) + date;
//create Folder
if (!Directory.Exists ("C:/Data/" + usernameGUI)) {
Directory.CreateDirectory ("C:/Data/" + usernameGUI);
}
//save to textfile
TextFile.SaveTextFile ("C:/Data/" + usernameGUI, "test");
}
}
Answer by Sebas · Nov 03, 2009 at 08:47 AM
Well, that much about my first question on here. I think I can answer it myself. I forgot to specify the filename. While the path itself was correct, I forgot to add "/" + usernameGUI + ".txt" to the end. Writing to a directory and not to a file most likely won't work.
TextFile.SaveTextFile ("C:/Data/" + usernameGUI + "/test.txt", "test");
--> works
I thought I tried that before, but it seems like I was missing a "/"or something. Thanks anyways.
Sebas