// http://www.dotnet247.com/247reference/msgs/21/108780.aspx public static string GetFileOwner( string filePath) { filePath = CheckAddLongPathPrefix(filePath); IntPtr pZero; IntPtr pSid; IntPtr psd; // Not used here var errorReturn = PInvokeHelper.GetNamedSecurityInfo( filePath, PInvokeHelper.SeFileObject, PInvokeHelper.OwnerSecurityInformation, out pSid, out pZero, out pZero, out pZero, out psd); if (errorReturn == 0) { const int bufferSize = 64; var buffer = new StringBuilder(); var accounLength = bufferSize; var domainLength = bufferSize; int sidNameUse; var account = new StringBuilder(bufferSize); var domain = new StringBuilder(bufferSize); errorReturn = PInvokeHelper.LookupAccountSid( null, pSid, account, ref accounLength, domain, ref domainLength, out sidNameUse); if (errorReturn == 0) { // http://msdn.microsoft.com/en-us/library/ms681382(VS.85).aspx. var lastWin32Error = Marshal.GetLastWin32Error(); throw new Win32Exception( lastWin32Error, string.Format( Resources.ErrorLookingUpSid, lastWin32Error, filePath, CheckAddDotEnd(new Win32Exception(lastWin32Error).Message))); } else { buffer.Append(domain); buffer.Append(@"\"); buffer.Append(account); return(buffer.ToString()); } } else { // http://msdn.microsoft.com/en-us/library/ms681382(VS.85).aspx. var lastWin32Error = Marshal.GetLastWin32Error(); throw new Win32Exception( lastWin32Error, string.Format( Resources.ErrorGettingSecurityInfo, lastWin32Error, filePath, CheckAddDotEnd(new Win32Exception(lastWin32Error).Message))); } }