Exemplo n.º 1
0
        /*        public ArrayList GetNetworkShareFoldersList(string serverName)
         *      {
         *          //List<string> shares = new List<string>();
         *          ArrayList shares = new ArrayList();
         *
         *          try
         *          {
         *              // do not use ConnectionOptions to get shares from local machine
         *              ConnectionOptions connectionOptions = new ConnectionOptions();
         *              connectionOptions.Username = @"\filer";
         *              connectionOptions.Password = "******";
         *              connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
         *
         *              ManagementScope scope = new ManagementScope(serverName + "root\\CIMV2", connectionOptions);
         *              scope.Connect();
         *
         *              ManagementObjectSearcher worker = new ManagementObjectSearcher(scope, new ObjectQuery("select Name from win32_share"));
         *
         *              foreach (ManagementObject share in worker.Get())
         *              {
         *                  DirectoryBrowse aDir = new DirectoryBrowse();
         *                  aDir.sPath = serverName + share["Name"].ToString() + "\\";
         *                  aDir.sName = share["Name"].ToString();
         *                  shares.Add(aDir);
         *              }
         *          }
         *          catch(Exception ex)
         *          {
         *              Console.WriteLine("Getnetworkshare: " + ex.Message);
         *          }
         *
         *          return shares;
         *      }*/


        //Return an array based on the path
        public ArrayList GetDirectoryList()
        {
            ArrayList aFileList = new ArrayList();

            //Check if we have a \\xxx\ path selected
            if (sCurrentPath == sDriveSelected)
            {
                if (sDriveSelected.IndexOf(@"\\") != -1)
                {
                    return(GetNetworkShareFoldersList(sCurrentPath));
                }
            }

            if (Directory.Exists(sCurrentPath))
            {
                DirectoryInfo[] dirArray = null;

                try
                {
                    dirArray = new DirectoryInfo(sCurrentPath)
                               .GetDirectories("*")
                               .ToArray();
                }
                catch (Exception ex) //Usally access denied, then return nothing...
                {
                    SetErrorString(@"GetDirectoryList exception: " + ex.Message);
                    //Console.WriteLine("Exception: " + ex.Message);
                    return(new ArrayList());
                }

                //Check if we are going to add ".." to the top
                if (sCurrentPath != sDriveSelected)
                {
                    DirectoryBrowse aDots = new DirectoryBrowse();
                    aDots.sPath = "..";
                    aDots.sName = "..";
                    aFileList.Add(aDots);
                }

                foreach (DirectoryInfo sDir in dirArray)
                {
                    DirectoryBrowse aDir = new DirectoryBrowse();
                    aDir.sPath = sDir.FullName;
                    aDir.sName = sDir.Name.Substring(sDir.Name.LastIndexOf('\\') + 1);
                    aFileList.Add(aDir);
                }
            }

            return(aFileList);
        }
Exemplo n.º 2
0
        public ArrayList GetNetworkShareFoldersList(string serverName)
        {
            ArrayList shares = new ArrayList();

            //Split each and go back one
            string[] parts = serverName.Split('\\');

            //Could not happend if stated as \\server\path\
            if (parts.Length < 3)
            {
                SetErrorString(@"GetNetworkShareFoldersList did not get a \\server\path as expected");
                return(shares);
            }

            string          server = @"\\" + parts[2] + @"\";
            ShareCollection shi    = ShareCollection.LocalShares;

            if (server != null && server.Trim().Length > 0)
            {
                shi = ShareCollection.GetShares(server);
                if (shi != null)
                {
                    foreach (Share si in shi)
                    {
                        //We only want the disks
                        if (si.ShareType == ShareType.Disk || si.ShareType == ShareType.Special)
                        {
                            DirectoryBrowse aDir = new DirectoryBrowse();
                            aDir.sPath = si.Root.FullName + @"\";
                            aDir.sName = si.NetName;
                            shares.Add(aDir);
                        }
                    }
                }
                else
                {
                    SetErrorString(@"Unable to enumerate the shares on " + server + " - Make sure the machine exists and that you have permission to access it");
                    return(new ArrayList());
                }
            }
            return(shares);
        }