Burn Selected Folders/Files in CD-Rom

Dani_S 3,911 Reputation points
2025-01-05T12:14:51.6766667+00:00

Hi,

I have treeview of drive/folders + files as you see .

While pressing on copy command I copied the selected files to temp folder.

I want code that will burn this files with all folder structure not only the files to CD-ROM.

Can you please supply all code ?

Thanks in advance,

User's image

1.I found this code in ChatGPT , it handel only files with no folders structure !!

how can i fixed it.

2.Does this code also take will work with impersonation?

using System;

using System.IO;

using System.Runtime.InteropServices;

using IMAPI2.Interop;

namespace CD_Burner

{
class Program

{

    static void Main(string[] args)

    {

        // Set up the IMAPI2 interfaces for writing

        IDiscFormat2Data discFormat = new DiscFormat2Data();

        IDiscRecorder2 discRecorder = new DiscRecorder2();

        IDiscMaster2 discMaster = new DiscMaster2();

        try

        {

            // Initialize the Disc Recorder

            string[] availableDevices = discMaster.GetDiscs();

            discRecorder = discMaster.Item(0); // Select the first available device

            // Prepare the data for burning

            string[] filesToBurn = Directory.GetFiles(@"C:\Path\To\Your\Files");

            // Create a writeable image of the files

            IFileSystemImage fileSystemImage = new FileSystemImage();

            foreach (var file in filesToBurn)

            {

                fileSystemImage.AddFile(file);

            }

            // Begin the burn process

            discFormat.WriteMedia(fileSystemImage, discRecorder);

            Console.WriteLine("Burning completed successfully!");

        }

        catch (Exception ex)

        {

            Console.WriteLine("An error occurred: " + ex.Message);

        }

    }

}
}
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,807 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,121 Reputation points
    2025-01-05T17:57:21.1433333+00:00

    The code is using the pinvoke to call win32 imapi2fs api. To add a directory see

    https://learn.microsoft.com/en-us/windows/win32/api/imapi2fs/nf-imapi2fs-ifilesystemimage-createdirectoryitem


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.