ArgusTV.Recorders.Common.ShareExplorer.GetShareInfo9x C# (CSharp) Method

GetShareInfo9x() private static method

private static GetShareInfo9x ( string serverName, IList shareTypes ) : List
serverName string
shareTypes IList
return List
        private static List<ShareInfo> GetShareInfo9x(string serverName, IList<ShareType> shareTypes)
        {
            int level = 50;
            int nRet = 0;
            ushort entriesRead, totalEntries;
            List<ShareInfo> shareInfoList = new List<ShareInfo>();

            Type t = typeof(SHARE_INFO_50);
            int size = Marshal.SizeOf(t);
            ushort cbBuffer = (ushort)(MAX_SI50_ENTRIES * size);
            //On Win9x, must allocate buffer before calling API
            IntPtr pBuffer = Marshal.AllocHGlobal(cbBuffer);

            try
            {
                nRet = NetShareEnum(serverName, level, pBuffer, cbBuffer, out entriesRead, out totalEntries);

                if (nRet == ERROR_WRONG_LEVEL)
                {
                    level = 1;
                    t = typeof(SHARE_INFO_1_9x);
                    size = Marshal.SizeOf(t);
                    nRet = NetShareEnum(serverName, level, pBuffer, cbBuffer, out entriesRead, out totalEntries);
                }

                if (nRet == NO_ERROR || nRet == ERROR_MORE_DATA)
                {
                    for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += size)
                    {
                        IntPtr pItem = new IntPtr(lpItem);

                        if (level == 1)
                        {
                            SHARE_INFO_1_9x shareInfo = (SHARE_INFO_1_9x)Marshal.PtrToStructure(pItem, t);
                            if (shareTypes.Contains(shareInfo.ShareType))
                            {
                                shareInfoList.Add(new ShareInfo(serverName, shareInfo.NetName, string.Empty, shareInfo.ShareType, shareInfo.Remark));
                            }
                        }
                        else
                        {
                            SHARE_INFO_50 shareInfo = (SHARE_INFO_50)Marshal.PtrToStructure(pItem, t);
                            if (shareTypes.Contains(shareInfo.ShareType))
                            {
                                shareInfoList.Add(new ShareInfo(serverName, shareInfo.NetName, shareInfo.Path, shareInfo.ShareType, shareInfo.Remark));
                            }
                        }
                    }
                }
            }
            finally
            {
                //Clean up buffer
                Marshal.FreeHGlobal(pBuffer);
            }
            return shareInfoList;
        }