private void Directory_Delete_CatchUnauthorizedAccessException_DirectoryHasDenyPermission(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); Console.WriteLine(); var gotException = false; var tempPath = UnitTestConstants.TempFolder; if (isNetwork) { tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath); } using (var rootDir = new TemporaryDirectory(tempPath, MethodBase.GetCurrentMethod().Name)) { var folder = System.IO.Directory.CreateDirectory(System.IO.Path.Combine(rootDir.Directory.FullName, "Existing Source Folder")); Console.WriteLine("Input Directory Path: [{0}]", folder); // Set DENY permission for current user. UnitTestConstants.FolderDenyPermission(true, folder.FullName); try { Alphaleonis.Win32.Filesystem.Directory.Delete(folder.FullName); } catch (Exception ex) { var exType = ex.GetType(); gotException = exType == typeof(UnauthorizedAccessException); Console.WriteLine("\n\tCaught {0} Exception: [{1}] {2}", gotException ? "EXPECTED" : "UNEXPECTED", exType.Name, ex.Message); } finally { // Remove DENY permission for current user. UnitTestConstants.FolderDenyPermission(false, folder.FullName); } } Assert.IsTrue(gotException, "The exception is not caught, but is expected to."); Console.WriteLine(); }
private void Directory_Delete_CatchUnauthorizedAccessException_FolderWithDenyPermission(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); var tempPath = System.IO.Path.GetTempPath(); if (isNetwork) { tempPath = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempPath); } using (var rootDir = new TemporaryDirectory(tempPath, "Directory.Delete")) { var folder = rootDir.RandomFileFullPath; Console.WriteLine("\nInput Directory Path: [{0}]\n", folder); System.IO.Directory.CreateDirectory(folder); // Create a temp folder and set DENY permission for current user. UnitTestConstants.FolderDenyPermission(true, folder); var gotException = false; try { Alphaleonis.Win32.Filesystem.Directory.Delete(folder); } catch (Exception ex) { var exName = ex.GetType().Name; gotException = exName.Equals("UnauthorizedAccessException", StringComparison.OrdinalIgnoreCase); Console.WriteLine("\n\tCaught Exception: [{0}] Message: [{1}]", exName, ex.Message); } Assert.IsTrue(gotException, "The exception is not caught, but is expected to."); // Remove DENY permission for current user and delete folder. UnitTestConstants.FolderDenyPermission(false, folder); } Console.WriteLine(); }