- Home /
Unauthorized Access Exeption (Access Denied) and XML.
I am trying to get my program to read the information of a game I created on Unity that I burned onto a CD-R. But whenever I do, I get the following error:
Unauthorized Access Exception: Access to the path "E:\GameInfo.xml" is denied.
Here's the script (c-sharp) I am using just for those who want to investigate (script not done):
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class MenuGameReader : MonoBehaviour {
//The two slashes ("//") mean unused code or side notes.
public string url = "File:///E:/GameInfo.xml";
public string path = "E:/GameInfo.xml";
public GameObject Canvas;
public Text GameTitleText;
[System.Serializable]
public class ProgramInfo {
[XmlAttribute("name")] public string name = "";
}
IEnumerator CheckForNewData()
{
yield return new WaitForSeconds(2);
}
IEnumerator Start () {
Canvas = GameObject.Find("Canvas");
GameTitleText = GameObject.Find("GameTitle").GetComponent<Text>();
Debug.Log("Hello");
WWW w = new WWW(url);
yield return w;
Debug.Log(w.text);
//yield return StartCoroutine("CheckForNewData");
Debug.Log("Reading disk, please wait...");
yield return w;
print(w.isDone);
if(w.isDone){
var xmlSerializer = new XmlSerializer(typeof(ProgramInfo));
//FileStream stream = File.Open("E:/GameInfo.xml", FileMode.Open);
var stream = File.Open(path, FileMode.Open);
var deserializedGameInfo = xmlSerializer.Deserialize(stream) as ProgramInfo;
stream.Close();
ProgramInfo programinfo = deserializedGameInfo;
Debug.Log(programinfo);
}
if (!string.IsNullOrEmpty(w.error)){
Debug.Log(w.error);
}
yield return new WaitForSeconds(4);
}
}
I have never received this issue with the "WWW" method, which I used to load THE SAME FILE! All I'm trying to do is to get my program to display XML, does anyone know if I can load the text data from the XML file through the WWW method? (P.S. The XML file is a local file.) I can show you the XML file upon request if you need it for assisting my issue.
Before you ask, disabling the Real-Time Virus scan and running as an administrator DIDN'T work for me.
Uhm it's 2:33am here. So you're lucky that you actually get an answer 44 $$anonymous$$utes after you posted the question. A lot questions here might take up to 24h until they get answered.
ps: Hardcoding a drive letter doesn't seem to be a good idea. On $$anonymous$$y PC i have the letters B, C, D, E, F, G, H, I, J, $$anonymous$$, L, $$anonymous$$, O, P and Q in use (N is my external HDD). Some are virtual CD drives some are card readers. So you can't be sure that "E" is actually a CD drive. In fact my "E" drive is a CF/$$anonymous$$D card reader.
Also if you're on windows keep in $$anonymous$$d that the normal file path use backslashes
"E:\\GameInfo.xml";
// or
@"E:\GameInfo.xml";
Answer by Bunny83 · Oct 07, 2017 at 12:32 AM
Well, just reading the documentation carefully actually explains everything. First of all you used File.Open(String, FileMode). If you scroll down to the "Exceptions" section you will find possible reasons why that method might throw an "UnauthorizedAccessException". It's actually the first case:
path specified a file that is read-only.
Since your file is located on a CD it's a read-only file. The Open method you used does use the FileAccess "ReadWrite". Since the file is read-only you can't open the file with ReadWrite access. You can only use Read access. So by using File.Open(String, FileMode, FileAccess) instead and passing FileAccess.Read as third parameter should solve the problem.
As you can read in the Exceptions section the "UnauthorizedAccessException" would still be thrown when:
path specified a file that is read-only and access is not Read.
So when using the FileAccess mode ReadWrite or Write you would get this exception on readonly files. But if you use Read it should work just fine.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need Help With XML Reading Class Initialization 1 Answer
Distribute terrain in zones 3 Answers
Problem calling variable across scripts (Closed) 1 Answer
Problem with starting on screen keyboard windows c# 1 Answer