如何将cdrom驱动器号添加到cdrecord的一行中

时间:2014-02-17 17:17:58

标签: c# vb.net

我是一名新手,正在使用Windows窗体应用程序gui进行cmd行程序“cdrecord”

我有一个组合框,在表格加载事件中,我在组合框中显示cdrom驱动器。

cdrecord使用此参数-scanbus我有两个驱动器,它们显示为:

'MATSHITA' 'DVD-R UJ-857E ' 'ZA0E' Removable CD-ROM
'HP ' 'DVD Writer 550r ' 'UH23' Removable CD-ROM

我还有代码来显示驱动器号。

如何将驱动器号添加到cdrecord名称前面? 喜欢:D:\'MATSHITA''DVD-R UJ-857E'“第一次开车” 喜欢:F:\'HP''DVD Writer 550r''UH23'可移动CD-ROM

这是我的整个代码。任何帮助将不胜感激。 感谢。

        Imports System.IO
        Imports System.Text

        Public Class Form1
        Public Function FlattenWhitespace(ByVal pText As String) As String
        Dim strFlattened As String

        ' Convert non-space whitespace characters to spaces
        pText = Replace(pText, Chr(160), " ")
        pText = Replace(pText, vbTab, " ")
        pText = Replace(pText, vbCr, " ")
        pText = Replace(pText, vbLf, " ")

        ' Flatten multiple spaces in a row to a single space
        Do
            strFlattened = Replace(pText, "  ", " ")
            If Len(strFlattened) = Len(pText) Then
                ' The length of the string before & after the replacement is the same
                ' This means there is no more replacing to do (i.e. the string is as "flat" as it is going to get)
                Exit Do  ' Quit the loop
            End If
            pText = strFlattened
        Loop
        FlattenWhitespace = strFlattened
    End Function

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim p As New Process, pStartPath As String
        pStartPath = Application.StartupPath + "\tools\"
        p.StartInfo.FileName = pStartPath + "cdrecord.exe"
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.Arguments = " -scanbus"
        p.Start()

        'Takes all the standerd out and puts in a string
        Dim outStr As String = p.StandardOutput.ReadToEnd()

        'Splits the output and puts into lines
        Dim listOfLines As New List(Of String)(Split(outStr, Environment.NewLine))

        'Use stringbuilder to remove lines that don't contain CD-ROM
        Dim sb As New StringBuilder

        For Each LineOfText In listOfLines
            If LineOfText.Contains("CD-ROM") Then
                sb.AppendLine(LineOfText.Trim)
                outStr = sb.ToString
            End If
        Next

        'We have a new list now that just contains our cdrom drives
        Dim s As String() = Split(outStr, Environment.NewLine)

        For Each itm In s

            'Removes extra spaces and chars thats not needed to display nicely in the combobox
            itm = FlattenWhitespace(itm.Substring(itm.LastIndexOf(")"c) + 1))

            ' I have two cdrom drives and they both show on the msgbox
            ' I can't figure out why I'm getting a third msgbox that is empty
            MsgBox(itm)

            If Not String.IsNullOrEmpty(itm) Then
                'Adds itm to combobox1 and is displayed like:  'MATSHITA' 'DVD-R UJ-857E ' 'ZA0E' Removable CD-ROM
                ' And the second drive in displayed like:      'HP ' 'DVD Writer 550r ' 'UH23' Removable CD-ROM
                ComboBox1.Items.Add(itm)
                ComboBox1.SelectedIndex = 0
            End If
        Next

        'gets cdrom drive letter and put it in combobox2
        Dim drvLtr As String
        For Each drive In DriveInfo.GetDrives()

            If drive.DriveType = DriveType.CDRom Then
                drvLtr = drive.ToString
                'My first cdrom drive letter is displayed as D:\
                'And the second one is:                      F:\
                ComboBox2.Items.Add(drvLtr)
                ComboBox2.SelectedIndex = 0
            End If
        Next
    End Sub
End Class

0 个答案:

没有答案