Delete folder from bit locker throw exception

Dani_S 4,501 Reputation points
2025-04-08T13:17:10.33+00:00

Hi,

The exception:

2025-04-08 16:09:51,501 [1] ERROR GssdDesktopClient.Maui.ViewModels.CopyViewModel - DeleteFolderCommandExcute System.IO.IOException: No mapping for the Unicode character exists in the target multi-byte code page. : 'E:\גגגגגגג'.    at System.IO.FileSystem.GetFindData(String fullPath, Boolean isDirectory, Boolean ignoreAccessDenied, WIN32_FIND_DATA& findData)    at System.IO.FileSystem.RemoveDirectory(String fullPath, Boolean recursive)    at System.IO.DirectoryInfo.Delete(Boolean recursive)    at GssdDesktopClient.Maui.ViewModels.ViewDevicesViewModel.<>c__DisplayClass95_0.<DeleteFolderCommandExcute>b__0() in C:\Applications\GssdDesktopClient\GssdDesktopClient.Maui\ViewModels\ViewDevicesViewModel.cs:line 1515    at System.Security.Principal.WindowsIdentity.<>c__DisplayClass74_0.<RunImpersonatedInternal>b__0(Object <p0>)    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location ---    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Security.Principal.WindowsIdentity.RunImpersonatedInternal(SafeAccessTokenHandle token, Action action)    at GssdDesktopClient.Maui.ViewModels.ViewDevicesViewModel.DeleteFolderCommandExcute(Object param) in C:\Applications\GssdDesktopClient\GssdDesktopClient.Maui\ViewModels\ViewDevicesViewModel.cs:line 1499

This is my code:

It happened in fat 32. and happened from times to times. It happened in windows 10 machine. I develope the code in .net 9 on maui. Steps: 1.insert the fat 32. 2.reading the inserted drive. 3.reading the selected path. 4.delete the selected path.

This is my code:

if (Directory.Exists(SelectedSourceItem.Path))

{

var viewDeviceItem = param as ViewDeviceItem;

if (viewDeviceItem != null)

{

var dir = new DirectoryInfo(viewDeviceItem.Path);

dir.Delete(true);

}

}

@Rob Caplan - MSFT regarding your answer:

Is it your solution using Directory.EnumerateFiles ?

using System;

using System.IO;

class Program

{

static void Main()

{

string directoryPath = @"C:\path\to\your\directory"; // Specify your directory path here

try

{

// Delete all files in the directory

foreach (var file in Directory.EnumerateFiles(directoryPath))

{

File.Delete(file);

}

// Delete all subdirectories in the directory

foreach (var subDirectory in Directory.EnumerateDirectories(directoryPath))

{

Directory.Delete(subDirectory, true); // true to delete subdirectories and files inside them

}

// Delete the directory itself

Directory.Delete(directoryPath, false); // false because we’ve already deleted the contents

Console.WriteLine("Directory and its contents have been deleted.");

}

catch (Exception ex)

{

Console.WriteLine($"An error occurred: {ex.Message}");

}

}

}

Developer technologies .NET .NET Runtime
{count} votes

Accepted answer
  1. Rob Caplan - MSFT 6,032 Reputation points Microsoft Employee Moderator
    2025-04-08T19:44:14.8466667+00:00

    Assuming that the problem comes when calling System.IO.DirectoryInfo.Delete on a Fat32 formatted drive (I'm guessing an external USB drive or such, and that BitLocker is not relevant), for a Directory (or its contents) with a name that includes non-Latin 1 characters (i.e. Hebrew), you are probably running into https://github.com/dotnet/runtime/issues/64789 (Weird FindFirstFileExW behavior on FAT32 #64789).

    As a workaround, per Dan Moseley's comments on that bug Directory.Enumerate will find the file. You could use this to iterate the directory's contents and delete the items individually.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.