Changeset 1711


Ignore:
Timestamp:
1/28/2010 4:16:14 AM (2 years ago)
Author:
lowjoel
Message:
  • Modified the VolumeInfo? class to allow enumeration of network drives
  • Define the ToString? function which will return the volume's mount point or it's volume ID depending on whether the volume is mounted
Location:
branches/eraser6/CodeReview
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/CodeReview/Eraser.Manager/FileSystem.cs

    r1709 r1711  
    308308 
    309309            throw new NotSupportedException(S._("The file system on the drive {0} is not " + 
    310                 "supported.", volume.IsMounted ? volume.MountPoints[0] : volume.VolumeId)); 
     310                "supported.", volume)); 
    311311        } 
    312312 
  • branches/eraser6/CodeReview/Eraser.Util/VolumeInfo.cs

    r1705 r1711  
    161161        { 
    162162            List<string> result = new List<string>(); 
     163            foreach (KeyValuePair<string, string> mountpoint in GetNetworkDrivesInternal()) 
     164                if (mountpoint.Value == VolumeId) 
     165                    result.Add(mountpoint.Key); 
     166 
     167            return result; 
     168        } 
     169 
     170        /// <summary> 
     171        /// Lists all the volumes in the system. 
     172        /// </summary> 
     173        /// <returns>Returns a list of volumes representing all volumes present in 
     174        /// the system.</returns> 
     175        public static IList<VolumeInfo> Volumes 
     176        { 
     177            get 
     178            { 
     179                List<VolumeInfo> result = new List<VolumeInfo>(); 
     180                StringBuilder nextVolume = new StringBuilder(NativeMethods.LongPath * sizeof(char)); 
     181                SafeHandle handle = NativeMethods.FindFirstVolume(nextVolume, NativeMethods.LongPath); 
     182                if (handle.IsInvalid) 
     183                    return result; 
     184 
     185                try 
     186                { 
     187                    //Iterate over the volume mountpoints 
     188                    do 
     189                        result.Add(new VolumeInfo(nextVolume.ToString())); 
     190                    while (NativeMethods.FindNextVolume(handle, nextVolume, NativeMethods.LongPath)); 
     191                } 
     192                finally 
     193                { 
     194                    //Close the handle 
     195                    NativeMethods.FindVolumeClose(handle); 
     196                } 
     197 
     198                return result.AsReadOnly(); 
     199            } 
     200        } 
     201 
     202        /// <summary> 
     203        /// Lists all mounted network drives on the current computer. 
     204        /// </summary> 
     205        public static IList<VolumeInfo> NetworkDrives 
     206        { 
     207            get 
     208            { 
     209                Dictionary<string, string> localToRemote = GetNetworkDrivesInternal(); 
     210                Dictionary<string, string> remoteToLocal = new Dictionary<string, string>(); 
     211 
     212                //Flip the dictionary to be indexed by value so we can map UNC paths to 
     213                //drive letters/mount points. 
     214                foreach (KeyValuePair<string, string> mountpoint in localToRemote) 
     215                { 
     216                    //If there are no UNC path for this current mount point, we just add it. 
     217                    if (!remoteToLocal.ContainsKey(mountpoint.Value)) 
     218                        remoteToLocal.Add(mountpoint.Value, mountpoint.Key); 
     219 
     220                    //Otherwise, we try to maintain the shortest path. 
     221                    else if (remoteToLocal[mountpoint.Value].Length > mountpoint.Key.Length) 
     222                        remoteToLocal[mountpoint.Value] = mountpoint.Key; 
     223                } 
     224 
     225                //Return the list of UNC paths mounted. 
     226                List<VolumeInfo> result = new List<VolumeInfo>(); 
     227                foreach (string uncPath in remoteToLocal.Keys) 
     228                    result.Add(new VolumeInfo(uncPath)); 
     229 
     230                return result.AsReadOnly(); 
     231            } 
     232        } 
     233 
     234        /// <summary> 
     235        /// Lists all mounted network drives on the current computer. The key is 
     236        /// the local path, the value is the remote path. 
     237        /// </summary> 
     238        private static Dictionary<string, string> GetNetworkDrivesInternal() 
     239        { 
     240            Dictionary<string, string> result = new Dictionary<string, string>(); 
    163241 
    164242            //Open an enumeration handle to list mount points. 
     
    167245                NativeMethods.RESOURCETYPE_DISK, 0, IntPtr.Zero, out enumHandle); 
    168246            if (errorCode != Win32ErrorCode.Success) 
    169                 throw new Win32Exception((int)errorCode); 
     247                throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 
    170248 
    171249            try 
     
    209287                                if (resource.lpRemoteName[resource.lpRemoteName.Length - 1] != '\\') 
    210288                                    resource.lpRemoteName += '\\'; 
    211  
    212                                 if (resource.lpRemoteName == VolumeId) 
    213                                     result.Add(resource.lpLocalName); 
     289                                result.Add(resource.lpLocalName, resource.lpRemoteName); 
    214290                            } 
    215291                        } 
     
    227303 
    228304            return result; 
    229         } 
    230  
    231         /// <summary> 
    232         /// Lists all the volumes in the system. 
    233         /// </summary> 
    234         /// <returns>Returns a list of volumes representing all volumes present in 
    235         /// the system.</returns> 
    236         public static IList<VolumeInfo> Volumes 
    237         { 
    238             get 
    239             { 
    240                 List<VolumeInfo> result = new List<VolumeInfo>(); 
    241                 StringBuilder nextVolume = new StringBuilder(NativeMethods.LongPath * sizeof(char)); 
    242                 SafeHandle handle = NativeMethods.FindFirstVolume(nextVolume, NativeMethods.LongPath); 
    243                 if (handle.IsInvalid) 
    244                     return result; 
    245  
    246                 try 
    247                 { 
    248                     //Iterate over the volume mountpoints 
    249                     do 
    250                         result.Add(new VolumeInfo(nextVolume.ToString())); 
    251                     while (NativeMethods.FindNextVolume(handle, nextVolume, NativeMethods.LongPath)); 
    252                 } 
    253                 finally 
    254                 { 
    255                     //Close the handle 
    256                     NativeMethods.FindVolumeClose(handle); 
    257                 } 
    258  
    259                 return result.AsReadOnly(); 
    260             } 
    261305        } 
    262306 
     
    666710        } 
    667711 
     712        /// <summary> 
     713        /// Gets the mount point of the volume, or the volume ID if the volume is 
     714        /// not currently mounted. 
     715        /// </summary> 
     716        /// <returns>A string containing the mount point of the volume or the volume 
     717        /// ID.</returns> 
     718        public override string ToString() 
     719        { 
     720            ReadOnlyCollection<string> mountPoints = MountPoints; 
     721            return mountPoints.Count == 0 ? VolumeId : mountPoints[0]; 
     722        } 
     723 
    668724        public VolumeLock LockVolume(FileStream stream) 
    669725        { 
Note: See TracChangeset for help on using the changeset viewer.