I am working on small scale deployment system for our in-house software that manages backups of several kinds of data. Part of the task is to backup certain folders, and I would like to backup the ACLs applied to those folders so that on restore the files still work as expected.
Prototyping some code, I have tried using .NET’s built-in XmlSerializer but to no avail.
Here is the example code:
public static byte[] SerializeFileACL(string path)
{
var acl = File.GetAccessControl(path, AccessControlSections.All);
using (var ms = new MemoryStream())
{
_fileSerializer.Serialize(ms, acl);
return ms.ToArray();
}
}
However this returns the same 143 bytes no matter what file I try it on. Is there a good way of serializing ACLs using .NET facilities? What am I doing wrong?
P.S.: Yes, I am aware that ACLs may not be applicable to other computers. This is not an issue for this project.
You can simply call the
NativeObjectSecurity.Persistmethod as described on MSDN to serialize this object. As one person mentioned the object does not contain aSerializableAttributeand thus you cannot serialize the object graph in the friendly way.