Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Burn CDs in C# With ICDBurn

0.00/5 (No votes)
18 Jan 2013 1  
Burn a CD using C#.

Introduction 

In contrast to C and C++, accessing ICDBurn from C# involves COM Interop (that part of the CLR that allows COM and .NET objects to interact with each other). This technology is visible in Listing 3's csburnd.cs source code via the System.Runtime.InteropServices namespace, the ICDBurn interface definition, and more.

Using the code

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

class CSburnD
{
    [DllImport("shfolder.dll")]
    static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder,
                                      IntPtr hToken, int dwFlags,
                                      StringBuilder pszPath);

    const int CSIDL_CDBURN_AREA = 0x3B;

    const int SHGFP_TYPE_CURRENT = 0;

    public static void Main(string[] args)
    {
        StringBuilder szPath = new StringBuilder(1024);
        if (SHGetFolderPath((IntPtr)0, CSIDL_CDBURN_AREA, (IntPtr)0,
            SHGFP_TYPE_CURRENT, szPath) != 0)
            Console.WriteLine("SHGetFolderPath() failure");
        else
            Console.WriteLine("SHGetFolderPath return value = " + szPath);

        Guid CLSID_CDBurn = new Guid("fbeb8a05-beee-4442-804e-409d6c4515e9");

        Type t = Type.GetTypeFromCLSID(CLSID_CDBurn);
        if (t == null)
        {
            Console.WriteLine("ICDBurn not supported by OS");
            return;
        }

        ICDBurn iface = (ICDBurn)Activator.CreateInstance(t);
        if (iface == null)
        {
            Console.WriteLine("Unable to obtain interface");
            return;
        }

        bool hasRecorder = false;
        iface.HasRecordableDrive(ref hasRecorder);
        Console.WriteLine("HasRecordableDrive return value = " + hasRecorder);

        if (hasRecorder)
        {
            StringBuilder driveLetter = new StringBuilder(4);
            iface.GetRecorderDriveLetter(driveLetter, 4);
            Console.WriteLine("GetRecorderDriveLetter return value = " +
                              driveLetter);
            iface.Burn((IntPtr)0);
        }
    }
}

[ComImport]
[Guid("3d73a659-e5d0-4d42-afc0-5121ba425c8d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ICDBurn
{
    void GetRecorderDriveLetter([MarshalAs(UnmanagedType.LPWStr)]
                                StringBuilder pszDrive, uint cch);
    void Burn(IntPtr hwnd);
    void HasRecordableDrive(ref bool HasRecorder);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here