0

The reason why I'm asking this is because .NET sees a folder as ReadOnly if any of the underlying files or folders are ReadOnly. Therefor this code:

if (!Properties.Settings.Default.searchReadOnly &&
    (diPath.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    writeable = false;

is always going to set writeable to false.

This is a problem if you need to know if the root folder is ReadOnly. The My Docments folder is not ReadOnly, but this is what's shown in the properties window:

My Documents properties

Any help would be appreciated.

EDIT

I tried to approach you suggested, but Documents still appears to have the ReadOnly flag set.

if (!Properties.Settings.Default.searchReadOnly &&
    diPath.Attributes.HasFlag(FileAttributes.ReadOnly)) // == true
    searchable = false;

How is this possible? What user is executing the code? I assume the actively logged in user? Again, assuming because I can write to the Documents folder, it can't have the ReadOnly flag set.

DerpyNerd
  • 4,743
  • 7
  • 41
  • 92
  • 1
    This SO thread should help: [link](http://stackoverflow.com/questions/7511592/check-if-folder-is-read-only-in-c-net) – sszarek Apr 23 '15 at 09:46

2 Answers2

1

this should help

use the System.IO.DirectoryInfo class:

var di = new DirectoryInfo(folderName);

if(di.Exists())
{
  if (di.Attributes.HasFlag(FileAttributes.ReadOnly))
  {
    //IsReadOnly...
  }

}

But

This state does not mean read only it's mean null state which means that the state has never changed and it's the default one.

this state means read only

enter image description here

Update if you have some sub folders that are marked read-only your root folder will be flagged read-only.

So try this check and apply as read-only to your folder than uncheck and apply and you will see that is no more marked as read-only

BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
0

I found a solution.

var writePermissionSet = new PermissionSet(PermissionState.None);
writePermissionSet.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write, path));

if (!Properties.Settings.Default.searchReadOnly &&
    !writePermissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))
    //diPath.Attributes.HasFlag(FileAttributes.ReadOnly))
    searchable = false;

This doens't really confirm the ReadOnly flag for the directory, but it does assure me that the user does not have Write permissions in this directory. It's kinda the ReadOnly I've been looking for :D

DerpyNerd
  • 4,743
  • 7
  • 41
  • 92