1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
const int ERROR_ACCESS_DENIED = 5;
const int ERROR_INVALID_LEVEL = 124; // unimplemented level for info
const int ERROR_INVALID_PARAMETER = 87;
const int ERROR_MORE_DATA = 234;
const int ERROR_NOT_ENOUGH_MEMORY = 8;
const int NERR_BufTooSmall = 2123; // The API return buffer is too small.
const int NERR_NetNameNotFound = 2310; // This shared resource does not exist.
const int NERR_Success = 0;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct SHARE_INFO_2
{
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_netname;
public uint shi2_type;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_remark;
public uint shi2_permissions;
public uint shi2_max_uses;
public uint shi2_current_uses;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_path;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_passwd;
}
static string FormatMessage(int errCode)
{
switch (errCode)
{
case ERROR_ACCESS_DENIED: return "The user does not have access to the requested information.";
case ERROR_INVALID_LEVEL: return "The value specified for the level parameter is invalid.";
case ERROR_INVALID_PARAMETER: return "The specified parameter is invalid.";
case ERROR_MORE_DATA: return "More entries are available. Specify a large enough buffer to receive all entries.";
case ERROR_NOT_ENOUGH_MEMORY: return "Insufficient memory is available.";
case NERR_BufTooSmall: return "The supplied buffer is too small.";
case NERR_NetNameNotFound: return "The share name does not exist.";
};
return null;
}
[DllImport("Netapi32", CharSet = CharSet.Auto)]
static extern int NetApiBufferFree(IntPtr Buffer);
[DllImport("Netapi32", CharSet = CharSet.Auto)]
static extern int NetShareGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.LPWStr)] string netname, int level, ref IntPtr bufptr);
/// <summary>
/// Retrieves the local path for the given server and share name.
/// </summary>
/// <remarks>serverName must start with \\</remarks>
static string NetShareGetPath(string serverName, string netName)
{
string path = null;
IntPtr ptr = IntPtr.Zero;
int errCode = NetShareGetInfo(serverName, netName, 2, ref ptr);
if (errCode == NERR_Success)
{
SHARE_INFO_2 shareInfo = (SHARE_INFO_2)
Marshal.PtrToStructure(ptr, typeof(SHARE_INFO_2));
path = shareInfo.shi2_path;
NetApiBufferFree(ptr);
}
else
Console.WriteLine(FormatMessage(errCode));
return path;
}
static string NetShareGetPermissions(string serverName, string netName)
{
string path = null;
IntPtr ptr = IntPtr.Zero;
int errCode = NetShareGetInfo(serverName, netName, 2, ref ptr);
if (errCode == NERR_Success)
{
SHARE_INFO_2 shareInfo = (SHARE_INFO_2)
Marshal.PtrToStructure(ptr, typeof(SHARE_INFO_2));
path = shareInfo.shi2_permissions.ToString();
NetApiBufferFree(ptr);
}
else
Console.WriteLine(FormatMessage(errCode));
return path;
} |
Partager