I think I found it:
System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(
"X:\\Somewhere\\MyDirectory"
);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(
new FileSystemAccessRule(
new System.Security.Principal.SecurityIdentifier(
System.Security.Principal.WellKnownSidType.BuiltinUsersSid,
null
),
FileSystemRights.DeleteSubdirectoriesAndFiles,
AccessControlType.Allow
)
);
dInfo.SetAccessControl(dSecurity);
The
BuiltinUsersSid
should make this work for all normal users (in some cases
WorldSid
could be helpful as "Everyone"). Of course, whoever executes that code has to have the right to change those permissions in order to succeed.
In my case I would call above code right after creating a directory. The caller would therefore be the owner. So he should be able to do whatever he wants.
I tried above code by creating two user accounts on my windows XP. As admin, I created a directory and run above code. Then, I created a file inside the directory.
As user1, I wasn't able to change the test file, but to delete it and create another one. As user2, I again couldn't change the new file, but delete it and create yet another one.
So
DeleteSubdirectoriesAndFiles
seems to cause just what you would expect from its name.