Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

C# access to path denied

  • 10-07-2014 5:48pm
    #1
    Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭


    HI. I have a c# program which takes a zip file, opens it, parses an XML file inside and does various operations on the data. The problem is when I go to parse the XML file I'm getting an access denied failure.
    The weird thing is, this does not occur when I run the app through visual studio. It only occurs after i run the application file directly, outside of VS.

    I've googled this a lot and have tried:
    • Directly setting permissions on this "new" unzipped folder to allow everyone to access/write to it.
    • The program is not running in a weird location. I'm running the application from a folder on my desktop
    • This still occurs if i run the app as admin
    • Using the same account I can manually open the folder and do anything i want
    • Other than creating the unzipper folder, no other method or class accessed/is holder the folder

    Simplified code snippet:
    //UNZIP
    ZipFile.ExtractToDirectory(path + ".zip", path + "_open");
    
    // open XML file
    XmlDocument manifestFile;
    manifestFile = new XmlDocument();
    manifestFile.Load(path + "_open\\file.xml");
    

    At that last line I get this error:
    "Access to the path XXXXX is denied"


Comments

  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    What's the path of the folder which contains the zip file(s)?


  • Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭EyeSight


    John_Mc wrote: »
    What's the path of the folder which contains the zip file(s)?

    The path I am using is: C:\Users\<myUsernam>\Desktop\testChecker\zipFile_open\AppxMetadata\file.xml
    App is running from here:
    C:\Users\<myUsernam>\Desktop\testChecker\MyApp.exe

    But my plan is to allow users to take my exe and run it from wherever they chose. I could specify they put it in a certain location if needed


  • Registered Users, Registered Users 2 Posts: 2,793 ✭✭✭John_Mc


    Can you try moving it to the root of the C or D or whatever drive? The desktop is specific to a user so you might be running into permissions issues with that.


  • Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭EyeSight


    Just ran it from C: and C:\testFolder and the issue is still showing up


  • Registered Users, Registered Users 2 Posts: 851 ✭✭✭TonyStark


    Does extract to directory recreate the folder and not set the permissions?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭EyeSight


    TonyStark wrote: »
    Does extract to directory recreate the folder and not set the permissions?

    Looking at the permissions on the folder it creates - In properties> Secutiry:
    It shows Authenticated users, admin and SYSTEM have permissions to read, write & execute.
    For some reason Users only have read & execute permissions. But that should allow them to access the path

    After encountering this issue I added this to the code(which doesn't get rid of the error.)
                 DirectorySecurity dir_security = Directory.GetAccessControl(path + "_open");
    
                IdentityReference everybodyIdentity = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
    
                FileSystemAccessRule rule = new FileSystemAccessRule(
                    everybodyIdentity,
                    FileSystemRights.FullControl,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow);
    
                Directory.SetAccessControl(path + "_open", dir_security);
    

    EDIT: I just looked at the folder permissions when running via VS(which works fine). Looks like the only difference is that I have write access. But in my code just above i am giving full access control. So I am not sure how else to do this


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Are you forgetting to actually add/set the Rule?
    [b]dir_security.AddAccessRule(rule);[/b]
    Directory.SetAccessControl(path + "_open", dir_security);
    


  • Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭BrokenArrows


    EyeSight wrote: »
    The path I am using is: C:\Users\<myUsernam>\Desktop\testChecker\zipFile_open\AppxMetadata\file.xml
    App is running from here:
    C:\Users\<myUsernam>\Desktop\testChecker\MyApp.exe

    But my plan is to allow users to take my exe and run it from wherever they chose. I could specify they put it in a certain location if needed

    You say your extracted directory is zipFile_open\AppxMetadata\file.xml but your opening from manifestFile.Load(path + "_open\\file.xml");. You're missing the AppxMetadata.


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    When you say
    This still occurs if i run the app as admin
    does this mean as an admin user, or by right clicking and choosing "Run as administrator". UAC may prevent programs accessing files even if the user running the program has access to them, and even when it's an admin user.


  • Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭EyeSight


    I have no idea why this fixed the problem, but instead of copying and unzipping the file in the same directory as where the app exe is, I changed it to be here:
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
    

    Now it works.
    Can a mod set this thread to "Answered" please? I don't think i have permissions to(ironically enough :p )


  • Advertisement
Advertisement