我有一个问题,我希望有人可以提供帮助。我有一个c#应用程序需要从驻留在Linux服务器上的文件中获取文件所有者信息。 .Net System.IO GetFileInfo抛出异常,WMI调用失败。我知道有PInvoke方法GetFileOwner但是pinvoke.net上的示例不完整且无法编译。有没有人有一个很好的完整示例或链接来查找此信息?
这是一个.Net c#3.5 windows应用程序,并且有权访问该文件,但我希望在完成剩余的处理之前获取所有者信息。
由于
答案 0 :(得分: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;
} }