/// <summary> /// Deletes the specified directory and, if indicated, any subdirectories in the directory. /// Absolute paths only!!! /// </summary> /// <param name="myPath">The absolute directory path to delete.</param> /// <param name="myRecursive">True to remove all content recursive as well.</param> public static void Delete(String myPath, Boolean myRecursive = false) { #if(__MonoCS__) Directory.Delete(myPath, myRecursive); #else myPath = PathHandler.GetFullPathInternal(myPath).TrimEnd(System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar); string demandDir = GetDemandDir(myPath, !myRecursive); //new FileIOPermission(FileIOPermissionAccess.Write, new string[] { demandDir }).Demand(); #region Check whether it exists WIN32_FILE_ATTRIBUTE_DATA data = new WIN32_FILE_ATTRIBUTE_DATA(); int errorCode = FileHandler.FillAttributeInfo(myPath, ref data, false, true); switch (errorCode) { case 0: break; case 2: throw new System.ComponentModel.Win32Exception(3); default: throw new System.ComponentModel.Win32Exception(errorCode); } if ((data.dwFileAttributes & 0x400) != 0) { myRecursive = false; } #endregion DeleteHelper(myPath, myRecursive); #endif }
internal static int FillAttributeInfo(string path, ref WIN32_FILE_ATTRIBUTE_DATA data, bool tryagain, bool returnErrorOnNotFound) { int lastWin32Error = 0; bool flag2 = NativeWin32Methods.GetFileAttributesExW(path, (GET_FILEEX_INFO_LEVELS)0, out data); if (!flag2) { lastWin32Error = Marshal.GetLastWin32Error(); //if (((lastWin32Error != 2) && (lastWin32Error != 3)) && (lastWin32Error != 0x15)) //{ // return FillAttributeInfo(path, ref data, true, returnErrorOnNotFound); //} if (!returnErrorOnNotFound) { lastWin32Error = 0; data.dwFileAttributes = -1; } } return lastWin32Error; }
/// <summary> /// The gets the attributes of <paramref name="myPath"/> and verify that it is a directory. /// </summary> /// <param name="path"></param> /// <returns></returns> internal static bool InternalExists(string myPath) { WIN32_FILE_ATTRIBUTE_DATA data = new WIN32_FILE_ATTRIBUTE_DATA(); return (((FileHandler.FillAttributeInfo(myPath, ref data, false, true) == 0) && ((int)data.dwFileAttributes != -1)) && (((int)data.dwFileAttributes & 0x10) != 0)); }
internal static extern bool GetFileAttributesExW(string lpFileName, GET_FILEEX_INFO_LEVELS fInfoLevelId, out WIN32_FILE_ATTRIBUTE_DATA fileData);