Getting the CD/DVD drive letter when using a SaveFileDialog (burning with IMAPI2)

553 Views Asked by At

I am using vb.net 2.0 framework to create a WinForms application that will be used in-house. One feature of this application is to allow users to export their work as text files. This all works fine for floppy (yes, some of my users still use these), USB and network locations, but I was recently asked to allow the files to be saved to CD/DVD. Since this application is only used on Windows 7 machines, I implemented IMAPI2. During my initial testing, I used a hard-coded path/file name and got everything working. The problem came in when I removed the hard-coded part and ran a test using the SaveFileDialog. This produced two undesirable results that I cannot find a way to work around.

  1. If I select the optical drive while a blank disc is in there, I get a windows generated popup asking how I want to use the disc (USB [Live File System| or CD/DVD Player [Mastered]). This will confuse my users. Is there a way to default this to Mastered and avoid this pop up?

  2. The path returned from the SaveFileDialog is not the optical drive, but rather the temporary CD Burn path--"c:\users[username]\AppData\Local\Microsoft\Windows\Burn\Burn\" in the case of my development computer (I have a feeling this is due my choosing Mastered in the popup I mentioned above). This results in the "You have files waiting to be burned to disc" balloon to popup in the system tray*. Is there a way to have the SaveFileDialog return the actual drive letter for the optical drive (along with any folder(s) the user might create on the CD/DVD)?

*The reason this happens is because I check for the drive type of the path returned from the SaveFileDialog. If the drive type is CDROM then I kick off a function that does the burning. Otherwise, I do a File.Copy() and since the destination returned from the SaveFileDialog is the burn folder, that's where the text files get copied to--resulting in the balloon notification.

Is there a better way to allow my users to save to any location (including optical drives)? Any pointers on how I can make this work would be greatly appreciated.

EDIT: A code sample has been requested. As mentioned in the comments, my users do not want to see the Windows generated dialog, so I built my own "Export Wizard". Here is a simplified version of the code I'm using (minus the actual CD Burning function, which I know does work).

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Function to get the drive type
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Int32
Public Enum DriveTypes
    Unknown = 0
    Invalid_or_Not_Mounted = 1
    Removable = 2
    Fixed = 3
    Remote = 4
    CDROM = 5
    RAMDisk = 6
End Enum

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Choose Location
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
    With Me.SaveFileDialog1
        .Filter = "Export Files|*.txt"
        .FileName = "Export Files.txt"
        r = .ShowDialog(Me)
        If r = Windows.Forms.DialogResult.OK Then
            Me.lblFileLocation.Text = .FileName.Substring(0, .FileName.LastIndexOf("\") + 1)
            ' As per their request, I show the users the location they chose
            ' before they finish up the export wizard
        End If
    End With
End Sub

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Export the data
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Export(byval fSourcePath as String, byval fDestName as String)
    Dim iDriveType As Me.DriveTypes = Me.GetDriveType(Directory.GetDirectoryRoot(Me.lblFileLocation.Text))
    If iDriveType = DriveTypes.CDROM Then
        Me.BurnCD(fSource, fDestName) ' This works when I pass in a hard-coded string that contains the drive letter for the optical drive.
    Else
        File.Copy(fSource, Me.lblFileLocation.Text & fDestName, True)
    End If
End Sub
0

There are 0 best solutions below