C# 从Linux文件中获取文件所有者

1 人关注

我有一个问题,希望有人能帮我解决。我有一个c#应用程序,需要从一个位于Linux服务器上的文件中获取文件所有者信息。.Net的System.IO GetFileInfo抛出了一个异常,WMI调用失败。我知道有一个PInvoke方法GetFileOwner,但是pinvoke.net上的例子并不完整,也不能编译。有没有人有一个好的完整的例子或链接来找到这些信息?

这是一个.Net c# 3.5的windows应用程序,确实有访问文件的权限,但是我想在它做其他处理之前得到所有者信息。

c#
linux
file
Consulting Mechanic
Consulting Mechanic
发布于 2009-09-23
1 个回答
Consulting Mechanic
Consulting Mechanic
发布于 2009-09-24
已采纳
0 人赞同

下面是我使用的代码样本。它在我的应用程序和我的环境中工作。应该在其他地方也能用。抱歉没有早点发布,我是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');
            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];
        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);
            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);
        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