How to delete all files & folders from USB & CD_ROM

Dani_S 4,091 Reputation points
2024-07-30T11:02:26.51+00:00

Hi,

I'm using net 8, I would like to delete all content of:

1.USB include bit locker.

2.CD-Rome

Can you supply full code how to do it?

Thanks,

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,105 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 48,876 Reputation points Microsoft Vendor
    2024-07-31T09:27:45.1333333+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

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.