15

I have come across the most frustrating error i have ever dealt with.
Im trying to parse an XML doc and I keep getting UnauthorizedAccessException when trying to load the doc. I have full permissions to the directory and file, its not read only, i cant see anything immediately wrong as to why i wouldnt be able to access the file.
I searched around for hours yesterday trying a bunch of different solutions that helped other people, none of them working for me.
I post my issue on StackOverflow yesterday with some details, hoping for some help or a "youre an idiot, Its because of this" type of comment but NO.
No answers.
This is the first time Ive really needed help with something, and the first time i havent gotten any response to a post.
Do i keep trying to fix this before the deadline on Sunday? Do i say fuck it and rewrite the xml in C# to meet my needs? Is there another option that i dont even know about yet?
I need a dev duck of some sort :/

Comments
  • 0
    Post SO link
  • 0
    What's the host OS?
    Does the directory you're accessing have the correct permissions?
  • 0
  • 0
    @njpugh90
    windows 10 for this project, and yes. I moved the file around to different directories too, even making new ones like the "Code" folder that its in now. Access denied in all of them.
  • 0
    Have you tried to load the file with a FileStream and then parse it with some XmlParser, or do you need XmlDocument?
  • 0
    @meowijuanas Which user is the executable running with? (see in task manager)
    Is an antivirus software running?
  • 0
  • 0
    @PonySlaystation i was running as my user at first, then i ran as administrator with same result.
    I do not have anti-virus software.
  • 1
    @meowijuanas Have you ckecked that windows defender is turned off as well?
  • 0
    @PonySlaystation Code should run fine even with Defender on
  • 1
  • 0
    @meowijuanas
    https://docs.microsoft.com/en-us/...

    "An UnauthorizedAccessException exception is typically thrown by a method that wraps a Windows API call. To find the reasons for the exception, examine the text of the exception object's Message property."

    What does the Message property spit out?
  • 2
    @Mitiko i didn't try file streams with XMLParser yet. I thought using XmlDocument seemed to be the easier and more direct route of accomplishing this (this is my first time parsing xml), and a lot of the documentation i was reading seemed to use XmlDocument over XmlParser.
    The XElement method in the link you sent could work, I didnt see that option in the documentation. Ill need to try that out
  • 0
    @meowijuanas https://docs.microsoft.com/en-us/...

    You can also load it from a stream
  • 2
    @Mitiko just whatever you do, don't cross the streams!
  • 1
    @PonySlaystation I came across that page as well and I am pretty sure the Message spit out the same crap about access being denied without any other information. Ill need to double check that though to make sure i didnt miss something
  • 0
    The reasons of such an exception are specified in here under UnauthorizedAccessException
    https://docs.microsoft.com/en-us/...
  • 0
    @njpugh90 What do you mean, cross the streams?
  • 1
    @Mitiko i have seen those options and the file is not a directory, it is not read-only, i do have permission to access it, so it has to be thats it not supported on my system, but that doesnt make sense to me. I feel like there should be some sort of disclaimer in the documentation about it not working sometimes or that my exception would give a little more information if it isnt supported.
  • 0
    @Mitiko or just some sort of clue as to why it isnt supported on my system and if there is a way i CAN make it work anyway
  • 0
    @meowijuanas Best thing we could do is dig in the source code, but it's getting pretty late here, and I'm tired
  • 1
    This kind of problems makes you realize, that you need a nap. Take a step back and realize how dumb your error was.

    As for me this error message totally seems to be an authorization problem. Maybe your software isn't able to get reading permission for that file. But I believe you probably know that already. 😅
  • 2
    Not sure if it helps but try and put the file within the same folder as your source code and call the load function with just the filename -
    doc.Load("YOURFILENAME.xml")

    If you're trying to test the same then try this

    var path = System.IO.Directory.GetParent(System.IO.Directory.GetParent(TestContext.CurrentContext.TestDirectory).ToString());

    doc.Load("YOURFILENAME.xml");
  • 0
    Should you really escape backslash in a @ string?
  • 0
    you tried locations other than the Users folder? like on the root of a secondary drive or a folder in the root of C?
    trying File.Open or File.RealAllText might be a good test as well
    You can also look at the security permissions on the folder and give read access to all users
  • 0
    @Voxera i didnt originally have it escaped, i tried it this way just to make sure that wasnt the cause of the exception for some silly reason. I never changed it back
  • 0
    @xalez i didnt try using a folder in the root of C, i assumed that would be permission-restrictive so i stayed in the Users dir. May try that.
    The directory it is in gives full access to system and all users
  • 0
    @meowijuanas have you tried just reading it as text with File.ReadAllText (if I remember correctly)

    That way you eliminate any part of the xml code and can focus on if its really a problem reading the file.

    If it works you could always load it from string.

    Also, how big is the file?

    Could it be so big it cannot be allocated?

    I had one such file that I had to switch to reading as a stream pulling individual nodes one at a time.
  • 0
    @anudroid i will give this a try! Thanks for the suggestion, i had it in same folder but used full path, got the exception the first time and assumed i couldnt be in that folder and moved it
  • 1
    The most stupid explanation would be some fuckup with the path name. Like some software that can't deal with spaces in path names (in 2018!), or some unicode thing with the path.
  • 0
    @Voxera File.ReadAllText() also gave the same exception. Access denied.
  • 1
    @Mitiko sorry, it was a reference to the Ghostbusters film
  • 0
    Some thoughts :

    Even if the user the program runs under has full access to the directory and the file, this will fail if it has no access to any parent folder. That is the tricky part with absolute paths.

    Windows can handle paths with "/" separators. Which eliminates all escape issues.

    Load the file with the a simple stream. If an exception is thrown, break and look in the stack trace where it was thrown. Print the message, not just examine it.
    (if that is possible)
  • 0
    To continue :

    If simple file IO works, make sure that any custom dll you use is executable by the user in question. On Windows, dynamic libraries are just executables with a different main function.
  • 0
    @Yamakuzure simple file I/O doesnt even work. I tried using a relative path last night with no luck. I cant seem to access any file for some reason, even ones i accessed in past projects.
    Due to sake of time and the amount of work i had left to do, i ended up abandoning the effort and redid this part of the project :/ i hate it though, this way wouldve been much nicer and cleaner i think
  • 0
    Check the folder permissions on the whole path, I believe you need read access to folders too. And make sure the running user is the one you want it to be.
  • 0
    @DucksCanCode i did all of these things :/ all files and directories in the path have full access and read privileges. There is only one user and admin :/
  • 0
    @meowijuanas I'm guessing the xml is valid... maybe write a quick separate utility to try and diagnose it?
  • 0
    @DucksCanCode i dont necessarily think thats the problem. I ended up testing just file i/o streams with normal text files and still get the error. So i dont think the issue lays with just this one xml file
  • 0
    @meowijuanas Sometimes I get stuck on a problem for hours and then it turns out restarting the system magically solves everything. Have you tried that?
  • 1
    @anudroid unfortunately i have, many times. I ran out of ideas to try on how to make it work. The deadline for the project was today, so i had to just abandon this issue and design it a different way. Annoys me though that i never got it fixed
Your Job Suck?
Get a Better Job
Add Comment