Esempio n. 1
0
        /// <summary>
        /// Enumerates the shares on Windows 9x
        /// </summary>
        /// <param name="server">The server name</param>
        /// <param name="shares">The ShareCollection</param>
        protected static void EnumerateShares9x(string server, ShareCollection shares, ref bool accessDenied)
        {
            int    level = 50;
            int    nRet = 0;
            ushort entriesRead, totalEntries;

            accessDenied = false;
            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(server, level, pBuffer, cbBuffer,
                                    out entriesRead, out totalEntries);

                if (ERROR_WRONG_LEVEL == nRet)
                {
                    level = 1;
                    t     = typeof(SHARE_INFO_1_9x);
                    size  = Marshal.SizeOf(t);

                    nRet = NetShareEnum(server, level, pBuffer, cbBuffer,
                                        out entriesRead, out totalEntries);
                }

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

                        if (1 == level)
                        {
                            SHARE_INFO_1_9x si = (SHARE_INFO_1_9x)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
                        }
                        else
                        {
                            SHARE_INFO_50 si = (SHARE_INFO_50)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
                        }
                    }
                }
                else
                {
                    Console.WriteLine(nRet);
                }
            }
            finally
            {
                //Clean up buffer
                Marshal.FreeHGlobal(pBuffer);
            }
        }
Esempio n. 2
0
        private string Seek()
        {
            StringBuilder sb = new StringBuilder();

            #region intialize search
            CompEnum domainComputers;
            domainComputers = new CompEnum(CompEnum.ServerType.SV_TYPE_DOMAIN_ENUM, null);
            #endregion

            #region search domains
            CompEnum networkComputers;
            for (int i = 0; i < domainComputers.Length; i++)
            {
                networkComputers = new CompEnum((uint)CompEnum.ServerType.SV_TYPE_SERVER + (uint)CompEnum.ServerType.SV_TYPE_PRINTQ_SERVER, domainComputers[i].Name);
                #region search computer shares
                ShareCollection shi = null;
                for (int j = 0; j < networkComputers.Length; j++)
                {
                    shi = ShareCollection.GetShares(networkComputers[j].Name);

                    if (!shi.AccessDenied)
                    {
                        foreach (Share anyShare in shi)
                        {
                            if (!anyShare.NetName.Contains("$"))
                            {
                                switch (anyShare.ShareType)
                                {
                                case ShareType.Device:
                                    sb.AppendLine(string.Format("Network Folder:{0}", anyShare.Root.FullName));
                                    break;

                                case ShareType.Disk:
                                    sb.AppendLine(string.Format("Network Disk:{0}", anyShare.Root.FullName));
                                    break;

                                case ShareType.Printer:
                                    sb.AppendLine(string.Format("Network Printer:{0}", anyShare.Root.FullName));
                                    break;

                                case ShareType.IPC:
                                    sb.AppendLine(string.Format("Network IPC:{0}", anyShare.Root.FullName));
                                    break;

                                default:
                                    sb.AppendLine(string.Format("Network Default:{0}", anyShare.Root.FullName));
                                    break;
                                }
                            }
                        }
                    }
                }
                #endregion
            }
            #endregion
            return(sb.ToString());
        }
Esempio n. 3
0
        /// <summary>
        /// Enumerates the shares on Windows NT
        /// </summary>
        /// <param name="server">The server name</param>
        /// <param name="shares">The ShareCollection</param>
        protected static void EnumerateSharesNT(string server, ShareCollection shares, ref bool accessDenied)
        {
            int    level = 2;
            int    entriesRead, totalEntries, nRet, hResume = 0;
            IntPtr pBuffer = IntPtr.Zero;

            try
            {
                nRet = NetShareEnum(server, level, out pBuffer, -1,
                                    out entriesRead, out totalEntries, ref hResume);

                if (ERROR_ACCESS_DENIED == nRet)
                {
                    //Need admin for level 2, drop to level 1
                    level = 1;
                    nRet  = NetShareEnum(server, level, out pBuffer, -1,
                                         out entriesRead, out totalEntries, ref hResume);
                    accessDenied = true;
                }

                if (NO_ERROR == nRet && entriesRead > 0)
                {
                    Type t      = (2 == level) ? typeof(SHARE_INFO_2) : typeof(SHARE_INFO_1);
                    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)
                        {
                            SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
                        }
                        else
                        {
                            SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
                        }
                    }
                }
            }
            finally
            {
                // Clean up buffer allocated by system
                if (IntPtr.Zero != pBuffer)
                {
                    NetApiBufferFree(pBuffer);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Enumerates the shares
        /// </summary>
        /// <param name="server">The server name</param>
        /// <param name="shares">The ShareCollection</param>
        protected static void EnumerateShares(string server, ShareCollection shares, ref bool accessDenied)
        {
            if (null != server && 0 != server.Length && !IsW2KUp)
            {
                server = server.ToUpper();

                // On NT4, 9x and Me, server has to start with "\\"
                if (!('\\' == server[0] && '\\' == server[1]))
                {
                    server = @"\\" + server;
                }
            }
            accessDenied = false;

            if (IsNT)
            {
                EnumerateSharesNT(server, shares, ref accessDenied);
            }
            else
            {
                EnumerateShares9x(server, shares, ref accessDenied);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Returns the local <see cref="Share"/> object with the best match
        /// to the specified path.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Share PathToShare(string fileName)
        {
            if (null == fileName || 0 == fileName.Length)
            {
                return(null);
            }

            fileName = Path.GetFullPath(fileName);
            if (!IsValidFilePath(fileName))
            {
                return(null);
            }

            ShareCollection shi = LocalShares;

            if (null == shi)
            {
                return(null);
            }
            else
            {
                return(shi[fileName]);
            }
        }
Esempio n. 6
0
 public ShareCollection GetSharesForComputer(string computerName)
 {
     return(ShareCollection.GetShares(computerName));
 }
Esempio n. 7
0
        /// <summary>
        /// Returns the UNC path for a mapped drive or local share.
        /// </summary>
        /// <param name="fileName">The path to map</param>
        /// <returns>The UNC path (if available)</returns>
        public static string PathToUnc(string fileName)
        {
            if (null == fileName || 0 == fileName.Length)
            {
                return(string.Empty);
            }

            fileName = Path.GetFullPath(fileName);
            if (!IsValidFilePath(fileName))
            {
                return(fileName);
            }

            int nRet = 0;
            UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
            int bufferSize          = Marshal.SizeOf(rni);

            nRet = WNetGetUniversalName(
                fileName, UNIVERSAL_NAME_INFO_LEVEL,
                ref rni, ref bufferSize);

            if (ERROR_MORE_DATA == nRet)
            {
                IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);;
                try
                {
                    nRet = WNetGetUniversalName(
                        fileName, UNIVERSAL_NAME_INFO_LEVEL,
                        pBuffer, ref bufferSize);

                    if (NO_ERROR == nRet)
                    {
                        rni = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer,
                                                                          typeof(UNIVERSAL_NAME_INFO));
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(pBuffer);
                }
            }

            switch (nRet)
            {
            case NO_ERROR:
                return(rni.lpUniversalName);

            case ERROR_NOT_CONNECTED:
                //Local file-name
                ShareCollection shi = LocalShares;
                if (null != shi)
                {
                    Share share = shi[fileName];
                    if (null != share)
                    {
                        string path = share.Path;
                        if (null != path && 0 != path.Length)
                        {
                            int index = path.Length;
                            if (Path.DirectorySeparatorChar != path[path.Length - 1])
                            {
                                index++;
                            }

                            if (index < fileName.Length)
                            {
                                fileName = fileName.Substring(index);
                            }
                            else
                            {
                                fileName = string.Empty;
                            }

                            fileName = Path.Combine(share.ToString(), fileName);
                        }
                    }
                }

                return(fileName);

            default:
                Console.WriteLine("Unknown return value: {0}", nRet);
                return(string.Empty);
            }
        }