I have had this problem before, but I can always fix it by going to the security tab, in properties, then Advanced, then eventually add Admin to the access permission list. But in this case, I actually have a windows forms app, in which, I use this code:

FileStream config = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); 

in C#.

This all worked fine inside of Visual Studio, but once I built the project, installed the program on my computer, and ran it, it created that folder.

For some reason I can't gain permission the usual way. It just gives me the message that "You do not have permission to view or edit this object's permission setting." I don't know why. Can someone please tell me how to either gain permission, or outright delete the folder? And what went wrong in my program and caused this to happen?

  • Is it on the root C:\? If so, create a subfolder. Jul 8, 2018 at 3:49

2 Answers 2


You could always take ownership of it, assuming you have the necessary admin access. Once you take ownership, you'll have to close the properties windows and then when you go back in you can change permissions. I can't speak to the changes in your code, but I'm betting you don't have 'ownership' of the folder.

  • Ok, something weird happened. That folder just disappeared... I don't know how or why, but it's not there anymore, I ran my program (the proper program, not just inside visual studio) and this time, it worked perfectly, It created the folder and the items inside, and it's functioning properly... What just happened?? Jul 8, 2018 at 11:11
  • If I had to guess, the folder that was there already existed under another account and once it was deleted, your program can now create it's own. That was a piece I couldn't figure out from your post entirely and that's the timeline for how this happened. Normally when you run any program, it will execute under the security token of the person executing it unless you take specific steps. Exceptions would be folders created by system accounts and what not.
    – D.J.
    Jul 8, 2018 at 12:33

You also need to call SetAccessControl to apply the changes.

ds = di.GetAccessControl();
di.SetAccessControl(ds); // nothing happens until you do this

It seems that the examples on MSDN are sorely lacking in detail, as discussed here. I hacked the code from this article to get the following which behaves well:

static bool SetAcl()
    FileSystemRights Rights = (FileSystemRights)0;
    Rights = FileSystemRights.FullControl;

    // *** Add Access Rule to the actual directory itself
    FileSystemAccessRule AccessRule = new FileSystemAccessRule("Users", Rights,

    DirectoryInfo Info = new DirectoryInfo(destinationDirectory);
    DirectorySecurity Security = Info.GetAccessControl(AccessControlSections.Access);

    bool Result = false;
    Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);

    if (!Result)
        return false;

    // *** Always allow objects to inherit on a directory
    InheritanceFlags iFlags = InheritanceFlags.ObjectInherit;
    iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

    // *** Add Access rule for the inheritance
    AccessRule = new FileSystemAccessRule("Users", Rights,
    Result = false;
    Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);

    if (!Result)
        return false;


    return true;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.