/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using Alphaleonis.Win32.Filesystem; using System; using System.Collections.Generic; using System.Net.NetworkInformation; using System.Security; namespace Alphaleonis.Win32.Network { partial class Host { /// Enumerates drives from the local host. /// drives from the local host. /// [SecurityCritical] public static IEnumerable EnumerateDrives() { return EnumerateDrivesCore(null, false); } /// Enumerates local drives from the specified host. /// drives from the specified host. /// /// The DNS or NetBIOS name of the remote server. refers to the local host. /// /// suppress any Exception that might be thrown as a result from a failure, /// such as unavailable resources. /// [SecurityCritical] public static IEnumerable EnumerateDrives(string host, bool continueOnException) { return EnumerateDrivesCore(host, continueOnException); } /// Enumerates local drives from the specified host. /// drives from the specified host. /// /// /// The DNS or NetBIOS name of the remote server. refers to the local host. /// /// suppress any Exception that might be thrown as a result from a failure, /// such as unavailable resources. /// [SecurityCritical] private static IEnumerable EnumerateDrivesCore(string host, bool continueOnException) { return EnumerateNetworkObjectCore(new FunctionData { EnumType = 1 }, (string structure, SafeGlobalMemoryBufferHandle buffer) => structure, (FunctionData functionData, out SafeGlobalMemoryBufferHandle buffer, int prefMaxLen, out uint entriesRead, out uint totalEntries, out uint resume) => { // When host == null, the local computer is used. // However, the resulting Host property will be empty. // So, explicitly state Environment.MachineName to prevent this. // Furthermore, the UNC prefix: \\ is not required and always removed. string stripUnc = Utils.IsNullOrWhiteSpace(host) ? Environment.MachineName : Path.GetRegularPathCore(host, GetFullPathOptions.CheckInvalidPathChars, false).Replace(Path.UncPrefix, string.Empty); return NativeMethods.NetServerDiskEnum(stripUnc, 0, out buffer, NativeMethods.MaxPreferredLength, out entriesRead, out totalEntries, out resume); }, continueOnException); } } }