Hi @Dani_S ,
First, CD-ROMs are usually read-only, so you can't delete files from them using software. If the CD-ROM is writable (such as a CD-RW), you'll need specific software or hardware to erase the disc.
As for BitLocker, using Directory
and File
classes, you can easily delete files from USB drives.
However, BitLocker encrypted drives require additional steps to unlock before you can delete files.
If in the unlocked state:
using System;
using System.IO;
class Program
{
static void Main()
{
// Replace 'E:\' with your USB drive letter
string usbDriveLetter = @"E:\";
if (Directory.Exists(usbDriveLetter))
{
DeleteAllFilesAndDirectories(usbDriveLetter);
Console.WriteLine("All files and directories deleted from the USB drive.");
}
else
{
Console.WriteLine("USB drive not found.");
}
}
static void DeleteAllFilesAndDirectories(string path)
{
// Delete all files
foreach (string file in Directory.GetFiles(path))
{
try
{
File.Delete(file);
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting file {file}: {ex.Message}");
}
}
// Recursively delete all directories
foreach (string directory in Directory.GetDirectories(path))
{
try
{
Directory.Delete(directory, true);
}
catch (Exception ex)
{
Console.WriteLine($"Error deleting directory {directory}: {ex.Message}");
}
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.