Reputation: 115
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
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
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