Recently, because the company's file management system needs to support long file names, that is, the limit of 260 characters, the company found relevant information online and found a dll written by a foreign cool man, based on the original author, we have added some of the methods we need to implement. The method is as follows:
If you need to retrieve the dll, leave your email address or upload it to csdn.
public static DirectorySecurity GetDirectorySecurity(string longname) { int length = 0; byte[] buffer;// = new byte[65536]; bool ret = Win32Interop.GetFileSecurity(longname, (int)(SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION ), null, 0, out length); if (length > 0) { buffer = new byte[length]; ret = Win32Interop.GetFileSecurity(longname, (int)(SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION), buffer, (uint)length, out length); DirectorySecurity pSD = new DirectorySecurity(); pSD.SetSecurityDescriptorBinaryForm(buffer); return pSD; } else { return null; } } public static DirectorySecurity GetDirectorySecurity(string longname, AccessControlSections sec) { int length = 0; byte[] buffer;// = new byte[65536]; bool ret = Win32Interop.GetFileSecurity(longname, (int)sec, null, 0, out length); if (length > 0) { buffer = new byte[length]; ret = Win32Interop.GetFileSecurity(longname, (int)sec, buffer, (uint)length, out length); DirectorySecurity pSD = new DirectorySecurity(); pSD.SetSecurityDescriptorBinaryForm(buffer); return pSD; } else { return null; } } public static FileSecurity GetFileSecurity(string longname) { int length = 0; byte[] buffer;// = new byte[65536]; bool ret = Win32Interop.GetFileSecurity(longname, (int)(SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION), null, 0, out length); if (length > 0) { buffer = new byte[length]; ret = Win32Interop.GetFileSecurity(longname, (int)(SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION), buffer, (uint)length, out length); FileSecurity pSD = new FileSecurity(); pSD.SetSecurityDescriptorBinaryForm(buffer); return pSD; } else { return null; } } public static FileSecurity GetFileSecurity(string longname,AccessControlSections sec) { int length = 0; byte[] buffer;// = new byte[65536]; bool ret = Win32Interop.GetFileSecurity(longname, (int)sec, null, 0, out length); if (length > 0) { buffer = new byte[length]; ret = Win32Interop.GetFileSecurity(longname, (int)sec, buffer, (uint)length, out length); FileSecurity pSD = new FileSecurity(); pSD.SetSecurityDescriptorBinaryForm(buffer); return pSD; } else { return null; } }