/Development/AlphaFS/Filesystem/NativeMethods.cs
http://alphafs.codeplex.com · C# · 2051 lines · 639 code · 276 blank · 1136 comment · 40 complexity · f0bc28e275fbb7a20f6c1c35e70d8c88 MD5 · raw file
Large files are truncated click here to view the full file
- /* Copyright (c) 2008-2012 Peter Palotas, 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 System;
- using System.Diagnostics.CodeAnalysis;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Security.AccessControl;
- using System.Text;
- using Alphaleonis.Win32.Security;
- using Microsoft.Win32.SafeHandles;
- using SecurityNativeMethods = Alphaleonis.Win32.Security.NativeMethods;
-
- namespace Alphaleonis.Win32.Filesystem
- {
- internal static class NativeMethods
- {
- #region Internal Utility
-
- internal static uint GetLowOrderDword(long lowPart)
- {
- return (uint)(lowPart & 0xFFFFFFFF);
- }
-
- internal static uint GetHighOrderDword(long highPart)
- {
- return (uint)((highPart >> 32) & 0xFFFFFFFF);
- }
-
- internal static long ToLong(uint highPart, uint lowPart)
- {
- return (((long)highPart) << 32) | (((long)lowPart) & 0xFFFFFFFF);
- }
-
- internal static ulong LuidToLong(SecurityNativeMethods.Luid luid)
- {
- ulong high = (((ulong)luid.HighPart) << 32);
- ulong low = (((ulong)luid.LowPart) & 0x00000000FFFFFFFF);
- return high | low;
- }
-
- internal static SecurityNativeMethods.Luid LongToLuid(ulong lluid)
- {
- return new SecurityNativeMethods.Luid { HighPart = (uint)(lluid >> 32), LowPart = (uint)(lluid & 0xFFFFFFFF) };
- }
-
- internal static bool ContainsFileAttributesFlag(FileAttributes attributes, FileAttributes hasAttribute)
- {
- return (attributes & hasAttribute) == hasAttribute;
- }
-
- internal static bool ContainsFileSystemAttributesFlag(VolumeInfoAttributes attributes, VolumeInfoAttributes hasAttribute)
- {
- return (attributes & hasAttribute) == hasAttribute;
- }
-
- /// <summary>Check is the current handle is not null, not closed and not invalid.</summary>
- /// <param name="handle">The current handle to check.</param>
- /// <param name="throwException"><c>true</c> will throw an <exception cref="Resources.HandleInvalid"/>, <c>false</c> will not raise this exception..</param>
- /// <returns><c>true</c> on success, <c>false</c> otherwise.</returns>
- internal static bool IsValidHandle(SafeHandle handle, bool throwException = true)
- {
- if (handle == null || handle.IsClosed || handle.IsInvalid)
- {
- if (throwException)
- throw new ArgumentException(Resources.HandleInvalid);
-
- return false;
- }
-
- return true;
- }
-
- /// <summary>Check is the current stream is not null, not closed and not invalid.</summary>
- /// <param name="stream">The current stream to check.</param>
- /// <param name="throwException"><c>true</c> will throw an <exception cref="Resources.HandleInvalid"/>, <c>false</c> will not raise this exception.</param>
- /// <returns><c>true</c> on success, <c>false</c> otherwise.</returns>
- internal static bool IsValidStream(FileStream stream, bool throwException = true)
- {
- if (stream == null || stream.SafeFileHandle == null || stream.SafeFileHandle.IsClosed || stream.SafeFileHandle.IsInvalid)
- {
- if (throwException)
- throw new ArgumentException(Resources.StreamInvalid);
-
- return false;
- }
-
- return true;
- }
-
- #region UnitSizeToText
-
- /// <summary>Convert a number of type T to string with UnitSize or Percentage suffixed.</summary>
- internal static string UnitSizeToText<T>(T numberOfBytes, params bool[] options)
- {
- // Suffixes
- // bool[0] = false = "MB", True = "MiB"
- // bool[1] = true = %
- bool useMebi = options != null && options.Any() && options[0];
- bool usePercent = options != null && options.Count() == 2 && options[1];
-
- string template = "{0:0.00}{1}";
- string sfx = useMebi ? "Bi" : "bytes";
-
- double bytes = Convert.ToDouble(numberOfBytes, CultureInfo.InvariantCulture);
-
- if (bytes >= 1125899906842624) { sfx = useMebi ? "PiB" : "PB"; bytes /= 1125899906842624; }
- else if (bytes >= 1099511627776) { sfx = useMebi ? "TiB" : "TB"; bytes /= 1099511627776; }
- else if (bytes >= 1073741824) { sfx = useMebi ? "GiB" : "GB"; bytes /= 1073741824; }
- else if (bytes >= 1048576) { sfx = useMebi ? "MiB" : "MB"; bytes /= 1048576; }
- else if (bytes >= 1024) { sfx = useMebi ? "KiB" : "KB"; bytes /= 1024; }
-
- else if (!usePercent)
- // Will return "512 bytes" instead of "512,00 bytes".
- template = "{0:0}{1}";
-
- return string.Format(CultureInfo.CurrentCulture, template, bytes, usePercent ? "%" : " " + sfx);
- }
-
- /// <summary>Calculates a percentage value.</summary>
- /// <param name="currentValue"></param>
- /// <param name="minimumValue"></param>
- /// <param name="maximumValue"></param>
- internal static double PercentCalculate(double currentValue, double minimumValue, double maximumValue)
- {
- return (currentValue < 0 || maximumValue <= 0) ? 0 : currentValue * 100 / (maximumValue - minimumValue);
- }
-
- #endregion // UnitSizeToText
-
- #region SetErrorMode
-
- /// <summary>Controls whether the system will handle the specified types of serious errors or whether the process will handle them.</summary>
- /// <remarks>Not yet implemented: Windows 7, callers should favor SetThreadErrorMode over SetErrorMode since it is less disruptive to the normal behavior of the system.</remarks>
- /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
- /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- internal struct ChangeErrorMode : IDisposable
- {
- private readonly NativeErrorMode _oldMode;
-
- internal ChangeErrorMode(NativeErrorMode mode)
- {
- _oldMode = SetErrorMode(mode);
- }
-
- void IDisposable.Dispose()
- {
- SetErrorMode(_oldMode);
- }
- }
-
- /// <summary>Controls whether the system will handle the specified types of serious errors or whether the process will handle them.</summary>
- /// <returns>The return value is the previous state of the error-mode bit attributes.</returns>
- /// <remarks>
- /// Because the error mode is set for the entire process, you must ensure that multi-threaded applications
- /// do not set different error-mode attributes. Doing so can lead to inconsistent error handling.
- /// </remarks>
- /// <remarks>SetLastError is set to false.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
- [return: MarshalAs(UnmanagedType.U4)]
- private static extern NativeErrorMode SetErrorMode(NativeErrorMode nativeErrorMode);
-
- #endregion // SetErrorMode
-
- #region Platform
-
- #region HasFunction
-
- /// <summary>Functions to check if a specific Win32 API function is available to differentiate between "Windows XP / Windows 2003 Server" and "Windows Vista / Windows 2008 Server" platforms.</summary>
- /// <returns>true if the function exists; otherwise, false. To get extended error information, call GetLastError.</returns>
- internal static bool HasFunction(string dllName, string functionName)
- {
- IntPtr dll = LoadLibrary(dllName);
-
- if (dll == IntPtr.Zero)
- return false;
-
- IntPtr result = GetProcAddress(dll, functionName);
- return result != IntPtr.Zero && Marshal.GetLastWin32Error() != Win32Errors.ERROR_PROC_NOT_FOUND;
- }
-
- #endregion // HasFunction
-
- #region LoadLibrary
-
- /// <summary>Loads the specified module into the address space of the calling process. The specified module may cause other modules to be loaded.</summary>
- /// <returns>
- /// If the function succeeds, the return value is a handle to the module.
- /// If the function fails, the return value is <see langword="null"/>.
- /// </returns>
- /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
- /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "LoadLibraryW")]
- private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPWStr)] string lpFileName);
-
- #endregion // LoadLibrary
-
- #region GetProcAddress
-
- /// <summary>Retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).</summary>
- /// <returns>
- /// If the function succeeds, the return value is the address of the exported function or variable.
- /// If the function fails, the return value is <see langword="null"/>.
- /// </returns>
- /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
- /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", MessageId = "1", Justification = "Stil need to investigate if CharSet.Unicode will not break this.")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetProcAddress")]
- private static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)] string procName);
- // Note: Leave as: UnmanagedType.LPStr, using UnmanagedType.LPWStr yields no results.
-
- #endregion // GetProcAddress
-
- #endregion // Platform
-
- #endregion // Internal Utility
-
- #region Constants
-
- #region Standard Values
-
- /// <summary>MaxPath = 260
- /// The specified path, file name, or both exceed the system-defined maximum length.
- /// For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
- /// </summary>
- internal const int MaxPath = 260;
-
- /// <summary>MaxPathUnicode = 32767</summary>
- internal const int MaxPathUnicode = 32767;
-
- /// <summary>DefaultFileEncoding = Encoding.UTF8; Default type of Encoding used for reading and writing files.</summary>
- internal static readonly Encoding DefaultFileEncoding = Encoding.UTF8;
-
- /// <summary>DefaultFileBufferSize = 4096; Default type buffer size used for reading and writing files.</summary>
- internal const int DefaultFileBufferSize = 4096;
-
- /// <summary>Combination of <see cref="CopyOptions.FailIfExists"/> and <see cref="CopyOptions.NoBuffering"/></summary>
- internal const CopyOptions CopyOptsFail = CopyOptions.FailIfExists | CopyOptions.NoBuffering;
-
- /// <summary>Combination of <see cref="CopyOptions.None"/> and <see cref="CopyOptions.NoBuffering"/></summary>
- internal const CopyOptions CopyOptsNone = CopyOptions.None | CopyOptions.NoBuffering;
-
- /// <summary>Combination of <see cref="MoveOptions.ReplaceExisting"/> and <see cref="MoveOptions.CopyAllowed"/></summary>
- internal const MoveOptions MoveOptsReplace = MoveOptions.ReplaceExisting | MoveOptions.CopyAllowed;
-
- #endregion // Standard Values
-
- #region Dos Device Flags (Used by DefineDosDevice)
-
- // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\DosDeviceAttributes.cs as DosDeviceAttributes enum
- // Used by DefineDosDevice
- //internal const uint DDD_RAW_TARGET_PATH = 0x00000001;
- //internal const uint DDD_REMOVE_DEFINITION = 0x00000002;
- //internal const uint DDD_EXACT_MATCH_ON_REMOVE = 0x00000004;
- //internal const uint DDD_NO_BROADCAST_SYSTEM = 0x00000008;
-
- #endregion // Dos Device Flags (Used by DefineDosDevice)
-
- #region File System Flags (Used by GetVolumeInformation)
-
- // 2012-02-14: Yomodo; Moved to Filesystem\Enumerations\VolumeInfoAttributes.cs as VolumeInfoAttributes enum
- // Used by GetVolumeInformation
- //internal const uint FILE_CASE_SENSITIVE_SEARCH = 0x00000001;
- //internal const uint FILE_CASE_PRESERVED_NAMES = 0x00000002;
- //internal const uint FILE_UNICODE_ON_DISK = 0x00000004;
- //internal const uint FILE_PERSISTENT_ACLS = 0x00000008;
- //internal const uint FILE_FILE_COMPRESSION = 0x00000010;
- //internal const uint FILE_VOLUME_QUOTAS = 0x00000020;
- //internal const uint FILE_SUPPORTS_SPARSE_FILES = 0x00000040;
- //internal const uint FILE_SUPPORTS_REPARSE_POINTS = 0x00000080;
- //internal const uint FILE_SUPPORTS_REMOTE_STORAGE = 0x00000100;
- //internal const uint FILE_VOLUME_IS_COMPRESSED = 0x00008000;
- //internal const uint FILE_SUPPORTS_OBJECT_IDS = 0x00010000;
- //internal const uint FILE_SUPPORTS_ENCRYPTION = 0x00020000;
- //internal const uint FILE_NAMED_STREAMS = 0x00040000;
- //internal const uint FILE_READ_ONLY_VOLUME = 0x00080000;
-
- #endregion // File System Flags (Used by GetVolumeInformation)
-
- #region Drive Types (Used by GetDriveType)
-
- // 2012-02-14: Yomodo; Obsolete, .NET DriveType enum is used instead.
- //internal const uint DRIVE_UNKNOWN = 0;
- //internal const uint DRIVE_NO_ROOT_DIR = 1;
- //internal const uint DRIVE_REMOVABLE = 2;
- //internal const uint DRIVE_FIXED = 3;
- //internal const uint DRIVE_REMOTE = 4;
- //internal const uint DRIVE_CDROM = 5;
- //internal const uint DRIVE_RAMDISK = 6;
-
- #endregion // Drive Types (Used by GetDriveType)
-
- #region File Access and Rights
-
- // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\FileSystemRights.cs as FileSystemRights enum
- // 2012-10-10: Yomodo; .NET already provides this enum.
- //internal const uint DRIVE_UNKNOWN = 0;
- //public const uint ACCESS_SYSTEM_SECURITY = 0x01000000;
- //public const uint DELETE = 0x00010000;
- //public const uint READ_CONTROL = 0x00020000;
- //public const uint WRITE_DAC = 0x00040000;
- //public const uint WRITE_OWNER = 0x00080000;
- //public const uint SYNCHRONIZE = 0x00100000;
- //public const uint STANDARD_RIGHTS_REQUIRED = 0x000F0000;
- //public const uint STANDARD_RIGHTS_READ = READ_CONTROL;
- //public const uint STANDARD_RIGHTS_WRITE = READ_CONTROL;
- //public const uint STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
- //public const uint STANDARD_RIGHTS_ALL = 0x001F0000;
- //public const uint SPECIFIC_RIGHTS_ALL = 0x0000FFFF;
-
- //public const uint FILE_READ_DATA = 0x0001;
- //public const uint FILE_LIST_DIRECTORY = 0x0001;
- //public const uint FILE_WRITE_DATA = 0x0002;
- //public const uint FILE_ADD_FILE = 0x0002;
- //public const uint FILE_APPEND_DATA = 0x0004;
- //public const uint FILE_ADD_SUBDIRECTORY = 0x0004;
- //public const uint FILE_CREATE_PIPE_INSTANCE = 0x0004;
- //public const uint FILE_READ_EA = 0x0008;
- //public const uint FILE_WRITE_EA = 0x0010;
- //public const uint FILE_EXECUTE = 0x0020;
- //public const uint FILE_TRAVERSE = 0x0020;
- //public const uint FILE_DELETE_CHILD = 0x0040;
- //public const uint FILE_READ_ATTRIBUTES = 0x0080;
- //public const uint FILE_WRITE_ATTRIBUTES = 0x0100;
- //public const uint FILE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF;
- //public const uint FILE_GENERIC_READ = STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE;
- //public const uint FILE_GENERIC_WRITE = STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE;
- //public const uint FILE_GENERIC_EXECUTE = STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE;
- //public const uint FILE_SHARE_READ = 0x00000001;
- //public const uint FILE_SHARE_WRITE = 0x00000002;
- //public const uint FILE_SHARE_DELETE = 0x00000004;
- //public const uint FILE_ATTRIBUTE_READONLY = 0x00000001;
- //public const uint FILE_ATTRIBUTE_HIDDEN = 0x00000002;
- //public const uint FILE_ATTRIBUTE_SYSTEM = 0x00000004;
- //public const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
- //public const uint FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
- //public const uint FILE_ATTRIBUTE_DEVICE = 0x00000040;
- //public const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
- //public const uint FILE_ATTRIBUTE_TEMPORARY = 0x00000100;
- //public const uint FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200;
- //public const uint FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400;
- //public const uint FILE_ATTRIBUTE_COMPRESSED = 0x00000800;
- //public const uint FILE_ATTRIBUTE_OFFLINE = 0x00001000;
- //public const uint FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000;
- //public const uint FILE_ATTRIBUTE_ENCRYPTED = 0x00004000;
- //public const uint FILE_ATTRIBUTE_VIRTUAL = 0x00010000;
- //public const uint FILE_NOTIFY_CHANGE_FILE_NAME = 0x00000001;
- //public const uint FILE_NOTIFY_CHANGE_DIR_NAME = 0x00000002;
- //public const uint FILE_NOTIFY_CHANGE_ATTRIBUTES = 0x00000004;
- //public const uint FILE_NOTIFY_CHANGE_SIZE = 0x00000008;
- //public const uint FILE_NOTIFY_CHANGE_LAST_WRITE = 0x00000010;
- //public const uint FILE_NOTIFY_CHANGE_LAST_ACCESS = 0x00000020;
- //public const uint FILE_NOTIFY_CHANGE_CREATION = 0x00000040;
- //public const uint FILE_NOTIFY_CHANGE_SECURITY = 0x00000100;
- //public const uint FILE_ACTION_ADDED = 0x00000001;
- //public const uint FILE_ACTION_REMOVED = 0x00000002;
- //public const uint FILE_ACTION_MODIFIED = 0x00000003;
- //public const uint FILE_ACTION_RENAMED_OLD_NAME = 0x00000004;
- //public const uint FILE_ACTION_RENAMED_NEW_NAME = 0x00000005;
- //public const uint FILE_SEQUENTIAL_WRITE_ONCE = 0x00100000;
- //public const uint FILE_SUPPORTS_TRANSACTIONS = 0x00200000;
-
- //public const uint REPLACEFILE_WRITE_THROUGH = 0x01;
- //public const uint REPLACEFILE_IGNORE_MERGE_ERRORS = 0x02;
- //public const uint REPLACEFILE_IGNORE_ACL_ERRORS = 0x04;
-
- #endregion // File Access and Rights
-
- #region Security
-
- // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\SecurityInformation.cs as SecurityInformation enum
- //public const int OWNER_SECURITY_INFORMATION = 0x00000001;
- //public const int GROUP_SECURITY_INFORMATION = 0x00000002;
- //public const int DACL_SECURITY_INFORMATION = 0x00000004;
- //public const int SACL_SECURITY_INFORMATION = 0x00000008;
- /* Not needed?
- public const uint LABEL_SECURITY_INFORMATION = 0x00000010;
- public const uint PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000;
- public const uint PROTECTED_SACL_SECURITY_INFORMATION = 0x40000000;
- public const uint UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000;
- public const uint UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000;
- */
- #endregion
-
- #region Backup
-
- // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\BackupStream.cs as BackupStreamAttributes enum
- // (Only moved these values, enum already existed)
- //public const uint STREAM_NORMAL_ATTRIBUTE = 0x00000000;
- //public const uint STREAM_MODIFIED_WHEN_READ = 0x00000001;
- //public const uint STREAM_CONTAINS_SECURITY = 0x00000002;
- //public const uint STREAM_CONTAINS_PROPERTIES = 0x00000004;
- //public const uint STREAM_SPARSE_ATTRIBUTE = 0x00000008;
-
- #endregion
-
- #endregion // Constants
-
- #region Network
-
- // 2012-01-31: Yomodo; Moved to: Filesystem\Enumerations\RemoteNameInfo.cs as RemoteNameInfo enum
- //[StructLayout(LayoutKind.Sequential)]
- //internal struct UNIVERSAL_NAME_INFO
- //{
- // /// <summary>
- // /// Network share name string that identifies a network resource.
- // /// A network share path identifies a network resource in an unambiguous, computer-independent manner.
- // /// You can pass the path to processes on other computers, allowing those processes to obtain access to the network resource.
- // /// </summary>
- // public string universalName;
- //}
- //public const uint UNIVERSAL_NAME_INFO_LEVEL = 0x00000001;
- //public const uint REMOTE_NAME_INFO_LEVEL = 0x00000002;
- //[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- //internal struct UNIVERSAL_NAME_INFO
- //{
- // [MarshalAs(UnmanagedType.LPWStr)] public string universalName;
- //}
-
- /// <summary>The WNetGetUniversalName function takes a drive-based path for a network resource and returns an information structure that contains a more universal form of the name.</summary>
- /// <returns>
- /// If the function succeeds, the return value is <see cref="Win32Errors.NO_ERROR"/>
- /// If the function fails, the return value is a system error code, <see cref="Win32Errors"/>
- /// </returns>
- /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
- /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("mpr.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "WNetGetUniversalNameW")]
- [return: MarshalAs(UnmanagedType.U4)]
- internal static extern uint WNetGetUniversalName([MarshalAs(UnmanagedType.LPWStr)] string lpLocalPath, [MarshalAs(UnmanagedType.U4)] uint dwInfoLevel, SafeGlobalMemoryBufferHandle lpBuffer, [MarshalAs(UnmanagedType.U4)] out uint lpBufferSize);
-
- #endregion // Network
-
- #region Volume Management
-
- #region DosDevice
-
- /// <summary>Defines, redefines, or deletes MS-DOS device names.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// To get extended error information call Win32Exception()
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DefineDosDeviceW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool DefineDosDevice(DosDeviceAttributes dwAttributes, [MarshalAs(UnmanagedType.LPWStr)] string lpDeviceName, [MarshalAs(UnmanagedType.LPWStr)] string lpTargetPath);
-
- /// <summary>Retrieves information about MS-DOS device names.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// To get extended error information call Win32Exception()
- /// If the buffer is too small, the function fails and the last error code is ERROR_INSUFFICIENT_BUFFER.
- /// </returns>
- /// <remarks>
- /// Windows Server 2003 and Windows XP: QueryDosDevice first searches the Local MS-DOS Device namespace for the specified device name.
- /// If the device name is not found, the function will then search the Global MS-DOS Device namespace.
- /// </remarks>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "QueryDosDeviceW")]
- [return: MarshalAs(UnmanagedType.U4)]
- internal static extern uint QueryDosDevice([MarshalAs(UnmanagedType.LPWStr)] string lpDeviceName, char[] lpTargetPath, [MarshalAs(UnmanagedType.U4)] uint ucchMax);
-
- #endregion // DosDevice
-
- #region Volume
-
- /// <summary>Retrieves the name of a volume on a computer. FindFirstVolume is used to begin scanning the volumes of a computer.</summary>
- /// <returns>
- /// If the function succeeds, the return value is a search handle used in a subsequent call to the FindNextVolume and FindVolumeClose functions.
- /// If the function fails to find any volumes, the return value is the INVALID_HANDLE_VALUE error code. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstVolumeW")]
- internal extern static SafeFindVolumeHandle FindFirstVolume(StringBuilder lpszVolumeName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
-
- /// <summary>Retrieves the name of a mounted folder on the specified volume. FindFirstVolumeMountPoint is used to begin scanning the mounted folders on a volume.</summary>
- /// <returns>
- /// If the function succeeds, the return value is a search handle used in a subsequent call to the FindNextVolumeMountPoint and FindVolumeMountPointClose functions.
- /// If the function fails to find a mounted folder on the volume, the return value is the INVALID_HANDLE_VALUE error code.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- /// <remarks>"lpszRootPathName" must end with a trailing backslash.</remarks>
- /// <remarks>
- /// Might not enumerate all: http://blogs.msdn.com/b/seealso/archive/2011/07/27/use-this-not-this-apis-to-avoid-all-together.aspx
- /// After Windows Vista, the API has changed. The short version of the issue you may encounter with FindFirstVolumeMountPoint is that unless you have a superuser account and permissions to the share, the mount point will not show up in the list.
- /// Your enumerated lists of mount points can be incomplete and you will not know when listed folders require permissions.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindFirstVolumeMountPointW")]
- internal extern static SafeFindVolumeMountPointHandle FindFirstVolumeMountPoint([MarshalAs(UnmanagedType.LPWStr)] string lpszRootPathName, StringBuilder lpszVolumeMountPoint, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
-
- /// <summary>Continues a volume search started by a call to the FindFirstVolume function. FindNextVolume finds one volume per call.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError. If no matching files can be found, the GetLastError function
- /// returns the ERROR_NO_MORE_FILES error code. In that case, close the search with the FindVolumeClose function.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindNextVolumeW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool FindNextVolume(SafeHandle hFindVolume, StringBuilder lpszVolumeName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
-
- /// <summary>Continues a mounted folder search started by a call to the FindFirstVolumeMountPoint function. FindNextVolumeMountPoint finds one mounted folder per call.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError. If no more mounted folders can be found, the GetLastError function returns the ERROR_NO_MORE_FILES error code.
- /// In that case, close the search with the FindVolumeMountPointClose function.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "FindNextVolumeMountPointW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool FindNextVolumeMountPoint(SafeHandle hFindVolume, StringBuilder lpszVolumeName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
-
- /// <summary>Closes the specified volume search handle.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool FindVolumeClose(IntPtr hFindVolume);
-
- /// <summary>Closes the specified mounted folder search handle.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool FindVolumeMountPointClose(IntPtr hFindVolume);
-
- /// <summary>Retrieves a volume GUID path for the volume that is associated with the specified volume mount point (drive letter, volume GUID path, or mounted folder).</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// To get extended error information call Win32Exception()
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- /// <remarks>"lpszVolumeMountPoint" must end with a trailing backslash.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumeNameForVolumeMountPointW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool GetVolumeNameForVolumeMountPoint([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeMountPoint, StringBuilder lpszVolumeName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
-
- /// <summary>Retrieves the volume mount point where the specified path is mounted.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// To get extended error information call Win32Exception()
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumePathNameW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool GetVolumePathName([MarshalAs(UnmanagedType.LPWStr)] string lpszFileName, StringBuilder lpszVolumePathName, [MarshalAs(UnmanagedType.U4)] uint cchBufferLength);
-
- /// <summary>Retrieves a list of drive letters and mounted folder paths for the specified volume.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumePathNamesForVolumeNameW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool GetVolumePathNamesForVolumeName([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeName, char[] lpszVolumePathNames, [MarshalAs(UnmanagedType.U4)] uint cchBuferLength, [MarshalAs(UnmanagedType.U4)] out uint lpcchReturnLength);
-
- /// <summary>Retrieves information about the file system and volume associated with the specified root directory.</summary>
- /// <returns>
- /// If all the requested information is retrieved, the return value is nonzero.
- /// If not all the requested information is retrieved, the return value is zero.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- /// <remarks>"lpRootPathName" must end with a trailing backslash.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumeInformationW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool GetVolumeInformation([MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName, StringBuilder lpVolumeNameBuffer, [MarshalAs(UnmanagedType.U4)] uint nVolumeNameSize, [MarshalAs(UnmanagedType.U4)] out uint lpVolumeSerialNumber, [MarshalAs(UnmanagedType.U4)] out uint lpMaximumComponentLength, [MarshalAs(UnmanagedType.U4)] out VolumeInfoAttributes lpFileSystemAttributes, StringBuilder lpFileSystemNameBuffer, [MarshalAs(UnmanagedType.U4)] uint nFileSystemNameSize);
-
- /// <summary>Retrieves information about the file system and volume associated with the specified file.</summary>
- /// <returns>
- /// If all the requested information is retrieved, the return value is nonzero.
- /// If not all the requested information is retrieved, the return value is zero.
- /// To get extended error information call Win32Exception()
- /// </returns>
- /// <remarks>Minimum supported client: Windows Vista</remarks>
- /// <remarks>Minimum supported server: Windows Server 2008</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetVolumeInformationByHandleW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool GetVolumeInformationByHandle(SafeHandle hFile, StringBuilder lpVolumeNameBuffer, [MarshalAs(UnmanagedType.U4)] uint nVolumeNameSize, [MarshalAs(UnmanagedType.U4)] out uint lpVolumeSerialNumber, [MarshalAs(UnmanagedType.U4)] out uint lpMaximumComponentLength, out VolumeInfoAttributes lpFileSystemAttributes, StringBuilder lpFileSystemNameBuffer, [MarshalAs(UnmanagedType.U4)] uint nFileSystemNameSize);
-
- /// <summary>Sets the label of a file system volume.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- /// <remarks>"lpRootPathName" must end with a trailing backslash.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetVolumeLabelW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool SetVolumeLabel([MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName, [MarshalAs(UnmanagedType.LPWStr)] string lpVolumeName);
-
- /// <summary>Associates a volume with a drive letter or a directory on another volume.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- /// <remarks>"lpszVolumeMountPoint" must end with a trailing backslash.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetVolumeMountPointW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool SetVolumeMountPoint([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeMountPoint, [MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeName);
-
- /// <summary>Deletes a drive letter or mounted folder.</summary>
- /// If all the requested information is retrieved, the return value is nonzero.
- /// If not all the requested information is retrieved, the return value is zero.
- /// To get extended error information call Win32Exception()
- /// <remarks>Deleting a mounted folder does not cause the underlying directory to be deleted.
- /// It's not an error to attempt to unmount a volume from a volume mount point when there is no volume actually mounted at that volume mount point.
- /// </remarks>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- /// <remarks>"lpszVolumeMountPoint" must end with a trailing backslash.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "DeleteVolumeMountPointW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal extern static bool DeleteVolumeMountPoint([MarshalAs(UnmanagedType.LPWStr)] string lpszVolumeMountPoint);
-
- #endregion // Volume
-
- #endregion // Volume Management
-
- #region Disk Management
-
- /// <summary>Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.</summary>
- /// <returns>The return value specifies the type of drive, which can be one of the following <see cref="DriveType"/> values.</returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetDriveTypeW")]
- [return: MarshalAs(UnmanagedType.U4)]
- internal extern static DriveType GetDriveType([MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName);
-
- /// <summary>Retrieves information about the specified disk, including the amount of free space on the disk.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>
- /// Symbolic link behavior; if the path points to a symbolic link, the operation is performed on the target.
- /// </remarks>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetDiskFreeSpaceW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool GetDiskFreeSpace([MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName, out ulong lpSectorsPerCluster, out ulong lpBytesPerSector, out ulong lpNumberOfFreeClusters, out ulong lpTotalNumberOfClusters);
-
- /// <summary>Retrieves information about the amount of space that is available on a disk volume, which is the total amount of space,
- /// the total amount of free space, and the total amount of free space available to the user that is associated with the calling thread.
- /// </summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>
- /// Symbolic link behavior; if the path points to a symbolic link, the operation is performed on the target.
- /// </remarks>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetDiskFreeSpaceExW")]
- [return: MarshalAs(UnmanagedType.Bool)]
- internal static extern bool GetDiskFreeSpaceEx([MarshalAs(UnmanagedType.LPWStr)] string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes);
-
- /// <summary>Retrieves a bitmask representing the currently available disk drives.</summary>
- /// <returns>
- /// If the function succeeds, the return value is a bitmask representing the currently available disk drives.
- /// Bit position 0 (the least-significant bit) is drive A, bit position 1 is drive B, bit position 2 is drive C, and so on.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- [return: MarshalAs(UnmanagedType.U4)]
- internal static extern uint GetLogicalDrives();
-
- #endregion // Disk Management
-
- #region Path
-
- #region GetLongPathName
-
- /// <summary>Converts the specified path to its long form.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetLongPathNameW")]
- [return: MarshalAs(UnmanagedType.U4)]
- internal static extern uint GetLongPathName([MarshalAs(UnmanagedType.LPWStr)] string lpszShortPath, StringBuilder lpszLongPath, [MarshalAs(UnmanagedType.U4)] uint cchBuffer);
-
- #endregion // GetLongPathName
-
- #region GetShortPathName
-
- /// <summary>Retrieves the short path form of the specified path.</summary>
- /// <returns>
- /// If the function succeeds, the return value is nonzero.
- /// If the function fails, the return value is zero. To get extended error information, call GetLastError.
- /// </returns>
- /// <remarks>Minimum supported client: Windows XP</remarks>
- /// <remarks>Minimum supported server: Windows Server 2003</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "GetShortPathNameW")]
- [return: MarshalAs(UnmanagedType.U4)]
- internal static extern uint GetShortPathName([MarshalAs(UnmanagedType.LPWStr)] string lpszLongPath, StringBuilder lpszShortPath, [MarshalAs(UnmanagedType.U4)] uint cchBuffer);
-
- #endregion // GetShortPathName
-
- #region Path/Url Conversion
-
- /// <summary>Converts a file URL to a Microsoft MS-DOS path.</summary>
- /// <returns>Type: HRESULT
- /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
- /// </returns>
- /// <remarks>Minimum supported client: Windows 2000 Professional</remarks>
- /// <remarks>Minimum supported server: Windows 2000 Server</remarks>
- /// <remarks>This function returns a standard COM error value, so set "PreserveSig" to <see langref="false"/> to automatically convert HRESULT or retval values to exceptions.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "PathCreateFromUrlW", PreserveSig = false)]
- [return: MarshalAs(UnmanagedType.U4)]
- internal static extern uint PathCreateFromUrl([MarshalAs(UnmanagedType.LPWStr)] string pszUrl, StringBuilder pszPath, ref uint pcchPath, uint dwFlags);
-
- /// <summary>Creates a path from a file URL.</summary>
- /// <returns>Type: HRESULT
- /// If this function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.
- /// </returns>
- /// <remarks>Minimum supported client: Windows Vista</remarks>
- /// <remarks>Minimum supported server: Windows Server 2008</remarks>
- /// <remarks>This function returns a standard COM error value, so set "PreserveSig" to <see langref="false"/> to automatically convert HRESULT or retval values to exceptions.</remarks>
- [SuppressMessage("Microsoft.Security", "CA5122:PInvokesShouldNotBeSafeCriticalFxCopRule")]
- [DllImport("shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode, PreserveSig = false)]
- [return: MarshalAs(UnmanagedType.U4)]
- internal static extern uint PathCreateFromUrlAlloc([MarshalAs(UnmanagedType.LPWStr)] string pszIn, out StringBuilder pszPath, uint dwFlags);
-
- /// <summary>Converts a Microsoft MS-DOS path to a canonicalized URL.</summary>
- /// <returns>Type: HRESULT
- /// Returns S_FALSE if pszPath is already in URL format. In this case, pszPath will simply be copied to ps…