private static IntPtr EnumerateSharesNT(string server, ShareCollection shares, ref int level, ref int hResume) { var nRet = NetShareEnum(server, level, out IntPtr pBuffer, -1, out int entriesRead, out int totalEntries, ref hResume); if (ErrorAccessDenied == nRet) { level = 1; nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); } if (NoError == nRet && entriesRead > 0) { var t = 2 == level ? typeof(ShareInfo2) : typeof(ShareInfo1); var offset = Marshal.SizeOf(t); for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += offset) { var pItem = new IntPtr(lpItem); if (1 == level) { var si = (ShareInfo1)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, si.ShareType); } else { var si = (ShareInfo2)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, si.ShareType); } } } return(pBuffer); }
// ReSharper disable InconsistentNaming static void EnumerateSharesNT(string server, ShareCollection shares) // ReSharper restore InconsistentNaming { int level = 2; int hResume = 0; IntPtr pBuffer = IntPtr.Zero; try { int entriesRead; int totalEntries; int nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); if (ErrorAccessDenied == nRet) { level = 1; nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); } if (NoError == nRet && entriesRead > 0) { Type t = 2 == level ? typeof(ShareInfo2) : typeof(ShareInfo1); int offset = Marshal.SizeOf(t); for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += offset) { IntPtr pItem = new IntPtr(lpItem); if (1 == level) { ShareInfo1 si = (ShareInfo1)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark); } else { ShareInfo2 si = (ShareInfo2)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, si.Path, si.ShareType, si.Remark); } } } } finally { if (IntPtr.Zero != pBuffer) { NetApiBufferFree(pBuffer); } } }
static void TryEnumerateSharesNT(string server, ShareCollection shares) { var level = 2; var hResume = 0; var pBuffer = IntPtr.Zero; try { pBuffer = EnumerateSharesNT(server, shares, ref level, ref hResume); } finally { if (IntPtr.Zero != pBuffer) { NetApiBufferFree(pBuffer); } } }
protected static void EnumerateShares(string server, ShareCollection shares) { EnumerateSharesNT(server, shares); }
// ReSharper disable InconsistentNaming static void EnumerateSharesNT(string server, ShareCollection shares) // ReSharper restore InconsistentNaming { int level = 2; int hResume = 0; IntPtr pBuffer = IntPtr.Zero; try { int entriesRead; int totalEntries; int nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); if(ErrorAccessDenied == nRet) { level = 1; nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume); } if(NoError == nRet && entriesRead > 0) { Type t = (2 == level) ? typeof(ShareInfo2) : typeof(ShareInfo1); int offset = Marshal.SizeOf(t); for(int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += offset) { IntPtr pItem = new IntPtr(lpItem); if(1 == level) { ShareInfo1 si = (ShareInfo1)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark); } else { ShareInfo2 si = (ShareInfo2)Marshal.PtrToStructure(pItem, t); shares.Add(si.NetName, si.Path, si.ShareType, si.Remark); } } } } finally { if(IntPtr.Zero != pBuffer) { NetApiBufferFree(pBuffer); } } }
protected static void EnumerateShares(string server, ShareCollection shares) => TryEnumerateSharesNT(server, shares);