public List <BaseShareInfo> EnumNetShares(string ServerName) { List <BaseShareInfo> ShareInfos = new List <BaseShareInfo>(); int entriesread = 0; int totalentries = 0; int resume_handle = 0; int nStructSize = Marshal.SizeOf(typeof(NetShareWrapper.SHARE_INFO_503)); IntPtr bufPtr = IntPtr.Zero; StringBuilder server = new StringBuilder(ServerName); try { int ret = NetShareWrapper.NetShareEnum(server, 503, ref bufPtr, ErrorConstants.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle); if (ret == ErrorConstants.ERROR_ACCESS_DENIED) { NetAPIConnection.EstablishConnectionWithPrompt(ServerName, String.Empty, String.Empty, ref ret); if (ret == ErrorConstants.NERR_Success) { ret = NetShareWrapper.NetShareEnum(server, 503, ref bufPtr, ErrorConstants.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle); } } if (ret == ErrorConstants.NERR_Success) { IntPtr currentPtr = bufPtr; for (int i = 0; i < entriesread; i++) { NetShareWrapper.SHARE_INFO_503 shi1 = (NetShareWrapper.SHARE_INFO_503)Marshal.PtrToStructure(currentPtr, typeof(NetShareWrapper.SHARE_INFO_503)); ShareInfos.Add(new BaseShareInfo() { ServerName = ServerName, ShareName = shi1.shi503_netname, SharePath = @"\\" + ServerName + @"\" + shi1.shi503_netname, DomainName = NetAPIConnection.GetDomainName(ServerName) }); currentPtr = (IntPtr)((ulong)currentPtr + (ulong)Marshal.SizeOf(shi1)); } NetAPICommon.NetApiBufferFree(bufPtr); } } catch (Exception ex) { AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error); } return(ShareInfos); }
public List <ShareInfo> EnumNetSharesWithPermission(string ServerName) { List <ShareInfo> ShareInfos = new List <ShareInfo>(); int entriesread = 0; int totalentries = 0; int resume_handle = 0; int nStructSize = Marshal.SizeOf(typeof(NetShareWrapper.SHARE_INFO_503)); IntPtr bufPtr = IntPtr.Zero; StringBuilder server = new StringBuilder(ServerName); try { int ret = NetShareWrapper.NetShareEnum(server, 503, ref bufPtr, ErrorConstants.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle); if (ret == ErrorConstants.ERROR_ACCESS_DENIED) { NetAPIConnection.EstablishConnectionWithPrompt(ServerName, String.Empty, String.Empty, ref ret); if (ret == ErrorConstants.NERR_Success) { ret = NetShareWrapper.NetShareEnum(server, 503, ref bufPtr, ErrorConstants.MAX_PREFERRED_LENGTH, ref entriesread, ref totalentries, ref resume_handle); } } if (ret == ErrorConstants.NERR_Success) { IntPtr currentPtr = bufPtr; for (int i = 0; i < entriesread; i++) { List <SharePermission> permissionItem = null; NetShareWrapper.SHARE_INFO_503 shi1 = (NetShareWrapper.SHARE_INFO_503)Marshal.PtrToStructure(currentPtr, typeof(NetShareWrapper.SHARE_INFO_503)); if (shi1.shi503_security_descriptor != null && shi1.shi503_security_descriptor != IntPtr.Zero) { permissionItem = GetSharePermission(currentPtr); } ShareInfos.Add(new ShareInfo() { CurrentUses = shi1.shi503_current_uses, Maxuses = shi1.shi503_max_uses, Name = shi1.shi503_netname, Path = shi1.shi503_path, Permissions = shi1.shi503_permissions, Remark = shi1.shi503_remark, Reserved = shi1.shi503_reserved, SecurityDescriptor = shi1.shi503_security_descriptor, ServerName = ServerName, Type = (int)shi1.shi503_type, SharePermissions = permissionItem != null ? permissionItem : new List <SharePermission>() }); currentPtr = (IntPtr)((ulong)currentPtr + (ulong)Marshal.SizeOf(shi1)); } NetAPICommon.NetApiBufferFree(bufPtr); } //else //{ // // ShareInfos.Add(new SHARE_INFO_1 NetShareWrapper.SHARE_INFO_503("ERROR=" + ret.ToString(), 10, string.Empty)); //} } catch (Exception ex) { AMTLogger.WriteToLog(ex.Message, MethodBase.GetCurrentMethod().Name, 0, AMTLogger.LogInfo.Error); } return(ShareInfos); }