2009-09-22 159 views
1

我有一個問題,我希望有人可以幫忙。我有一個c#應用程序需要從駐留在Linux服務器上的文件獲取文件所有者信息。 .Net System.IO GetFileInfo引發異常,WMI調用失敗。我知道有PInvoke方法GetFileOwner但是pinvoke.net上的示例並不完整,並且不能編譯。有沒有人有一個很好的完整例子或鏈接來找到這些信息?C#從Linux文件獲取文件所有者文件

這是一個.net c#3.5 windows應用程序,並且具有訪問該文件的權限,但是我希望在處理其餘部分之前獲取所有者信息。

謝謝

回答

2

下面是我使用的代碼示例。它適用於我的應用程序和我的環境。應該在別人身上工作。對不起,這不是更早發佈,是新的StackOverFlow。我希望這個帖子正確。

public class GetUserInfo{ 
private const int NAME_SIZE = 0x40; 

// Methods 
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] 
private static extern bool ConvertSidToStringSid(IntPtr Sid, ref IntPtr StringSid); 
public string ConvertSidToStringSidNT(IntPtr Sid) 
{ 
    string ans = string.Empty; 
    if (IsValidSid(Sid)) 
    { 
     ans = "S-1-"; 
     SID_IDENTIFIER_AUTHORITY psia = (SID_IDENTIFIER_AUTHORITY) Marshal.PtrToStructure(GetSidIdentifierAuthority(Sid), typeof(SID_IDENTIFIER_AUTHORITY)); 
     int num = Marshal.ReadInt16(GetSidSubAuthorityCount(Sid)); 
     if ((psia.Value[0] != 0) & (psia.Value[1] != 0)) 
     { 
      ans = ((ans + Conversion.Hex(psia.Value[0]) + Conversion.Hex(psia.Value[1]).PadLeft(2, '0')) + Conversion.Hex(psia.Value[2]).PadLeft(2, '0') + Conversion.Hex(psia.Value[3]).PadLeft(2, '0')) + Conversion.Hex(psia.Value[4]).PadLeft(2, '0') + Conversion.Hex(psia.Value[5]).PadLeft(2, '0'); 
     } 
     else 
     { 
      long top = psia.Value[5]; 
      top += psia.Value[4] * 0x100; 
      top += (psia.Value[3] * 0x100) * 0x100; 
      ans = ans + ((top + (((psia.Value[2] * 0x100) * 0x100) * 0x100))).ToString(); 
     } 
     int VB$t_i4$L0 = num - 1; 
     for (int i = 0; i <= VB$t_i4$L0; i++) 
     { 
      ans = ans + "-" + Marshal.ReadInt32(GetSidSubAuthority(Sid, i)).ToString(); 
     } 
    } 
    return ans; 
} 

public string GetFileOwner(string Path) 
{ 
    string MachineName; 
    IntPtr OwnerSid; 
    int peUse; 
    IntPtr SD; 
    string UserName; 
    IntPtr VB$t_struct$N0; 
    SE_OBJECT_TYPE ObjectType = SE_OBJECT_TYPE.SE_FILE_OBJECT; 
    if (GetNamedSecurityInfo(ref Path, ObjectType, SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, ref OwnerSid, ref VB$t_struct$N0, ref VB$t_struct$N0, ref VB$t_struct$N0, ref SD) != 0) 
    { 
     return "Error"; 
    } 
    Marshal.FreeHGlobal(SD); 
    if (Path.StartsWith(@"\\")) 
    { 
     MachineName = Path.Split(new char[] { '\\' })[2]; 
    } 
    else 
    { 
     MachineName = ""; 
    } 
    int name_len = 0x40; 
    int domain_len = 0x40; 
    string name = Strings.Space(name_len); 
    string domain_name = Strings.Space(domain_len); 
    if (!LookupAccountSid(ref MachineName, OwnerSid, ref name, ref name_len, ref domain_name, ref domain_len, ref peUse)) 
    { 
     string SidString; 
     if (Marshal.GetLastWin32Error() != 0x534) 
     { 
      return "Error"; 
     } 
     if (Environment.Version.Major == 4) 
     { 
      SidString = this.ConvertSidToStringSidNT(OwnerSid); 
     } 
     else 
     { 
      IntPtr StringPtr; 
      if (!ConvertSidToStringSid(OwnerSid, ref StringPtr)) 
      { 
       return "Error"; 
      } 
      SidString = Marshal.PtrToStringAuto(StringPtr); 
      Marshal.FreeHGlobal(StringPtr); 
     } 
     domain_len = 0; 
     name = SidString; 
     name_len = Strings.Len(name); 
    } 
    if (domain_len > 0) 
    { 
     UserName = Strings.Left(name, name_len); 
    } 
    else 
    { 
     UserName = Strings.Left(name, name_len); 
    } 
    return UserName; 
} 

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] 
private static extern int GetNamedSecurityInfo([MarshalAs(UnmanagedType.VBByRefStr)] ref string pObjectName, SE_OBJECT_TYPE ObjectType, SECURITY_INFORMATION SecurityInfo, ref IntPtr ppsidOwner, ref IntPtr ppsidGroup, ref IntPtr ppDacl, ref IntPtr ppSacl, ref IntPtr ppSecurityDescriptor); 
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] 
private static extern IntPtr GetSidIdentifierAuthority(IntPtr pSid); 
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] 
private static extern IntPtr GetSidSubAuthority(IntPtr pSid, int nSubAuthority); 
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] 
private static extern IntPtr GetSidSubAuthorityCount(IntPtr pSid); 
[DllImport("advapi32.dll", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)] 
private static extern bool IsValidSid(IntPtr pSid); 
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] 
private static extern bool LookupAccountSid([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpSystemName, IntPtr lpSid, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpName, ref int cchName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpReferenceDomainName, ref int cchReferencedDomainName, ref int peUse); 

// Nested Types 
private enum SE_OBJECT_TYPE 
{ 
    SE_UNKNOWN_OBJECT_TYPE, 
    SE_FILE_OBJECT, 
    SE_SERVICE, 
    SE_PRINTER, 
    SE_REGISTRY_KEY, 
    SE_LMSHARE, 
    SE_KERNEL_OBJECT, 
    SE_WINDOW_OBJECT, 
    SE_DS_OBJECT, 
    SE_DS_OBJECT_ALL, 
    SE_PROVIDER_DEFINED_OBJECT, 
    SE_WMIGUID_OBJECT, 
    SE_REGISTRY_WOW64_32 
} 

private enum SECURITY_INFORMATION 
{ 
    DACL_SECURITY_INFORMATION = 4, 
    GROUP_SECURITY_INFORMATION = 2, 
    OWNER_SECURITY_INFORMATION = 1, 
    PROTECTED_DACL_SECURITY_INFORMATION = 0x20, 
    PROTECTED_SACL_SECURITY_INFORMATION = 0x10, 
    SACL_SECURITY_INFORMATION = 8, 
    UNPROTECTED_DACL_SECURITY_INFORMATION = 0x80, 
    UNPROTECTED_SACL_SECURITY_INFORMATION = 0x40 
} 

[StructLayout(LayoutKind.Sequential)] 
private struct SID_IDENTIFIER_AUTHORITY 
{ 
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=6)] 
    public byte[] Value; 
} } 
+5

我並不需要它,但如果你發佈有目共睹的解決方案這將是很好。你永遠不知道,別人可能會有同樣的問題。 – 2009-09-24 14:17:23

+3

你真的應該在這裏發佈解決方案。對於任何人來說,收集更多的「plzsendtehcodez kthx」答案是沒有用的...... – sth 2009-11-07 18:35:08

+0

Stack Overflow的目的是收集相關編程相關問題的良好答案。您應該自動默認發佈實際解決方案,而不是指出存在解決方案。 – 2010-06-17 16:42:35