Add() protected method

protected Add ( string netName, string path, ShareType shareType, string remark ) : void
netName string
path string
shareType ShareType
remark string
return void
Exemplo n.º 1
0
        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);
                }
            }
        }
// 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);
                }
            }
        }