viktor.488
viktor.488

Reputation: 115

Getting System.UnauthorizedAccessException error when loading the XML file

Im getting System.UnauthorizedAccessException everytime. Im running it as adminstrator. But Its still gives error... Code:

XmlDocument doc = new XmlDocument();
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Test\\" + textBox1.Text;
doc.Load(path); //Getting error

Thanks.

System.UnauthorizedAccessException: Access denied to path 'C:\Users\x\Documents\Test\Hello'.

Upvotes: 0

Views: 1141

Answers (1)

TheGeneral
TheGeneral

Reputation: 81593

Firstly

use Path.Combine(String, String)

Combines two strings into a path.

Example

var myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

var dir = Path.Combine(myDocs,"Test");
var fileName =Path.Combine(dir, textBox1.Text); 

Debug.WriteLine(fileName);

doc.Load(fileName)

Secondly

look at the documentation

XmlDocument.Load Method

UnauthorizedAccessException filename specified a file that is read-only.

Double check your path it doesn't look like a filename. If it is a file without an extension (and it does exist), make sure its not read-only

System.UnauthorizedAccessException: Access denied to path

'C:\Users\x\Documents\Test\Hello'.

If you correct that you'll probably win

Upvotes: 2

Related Questions