如何使用VB.NET从CD驱动器打开文件夹?

时间:2010-07-17 17:39:41

标签: vb.net directory cd-drive

我正在尝试编写一个程序,在单击按钮时从CD磁盘打开文件夹。该程序将从CD运行,旨在打开某个文件夹。但是,我不能使用“shell”资源管理器....“”因为驱动器号将在不同的计算机之间发生变化。有没有办法直接从VB.NET中的CD打开文件夹

5 个答案:

答案 0 :(得分:2)

如果您知道您的程序是从CD启动的,那么很容易。只需阅读程序位置:

    Dim exePath As String = System.Reflection.Assembly.GetEntryAssembly().Location
    Dim drive As String = System.IO.Path.GetPathRoot(exePath)

答案 1 :(得分:1)

例如,如果我要执行一个文件:executable.exe; 在光盘驱动器E:\ executables \ executable.exe“)

代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strDrives() As String

        Dim drvInof As System.IO.DriveInfo

        'Get all drives on the computer

.       strDrives = System.IO.Directory.GetLogicalDrives

        'list all the drives

        For i As Int16 = 0 To strDrives.Length

            'Get drive info

            drvInof = New System.IO.DriveInfo(strDrives(i))

            'Check if it`s CDRom

            If drvInof.DriveType = IO.DriveType.CDRom Then

                'Run exe from the CDRom

                Try

                  'here we try to run from the cdrom we found the exe

                    Process.Start(drvInof.Name & "executables\executable.exe")

                Catch ex As Exception

                    'error handle if the exe is not found or anything else

                    MessageBox.Show(ex.ToString)

                End Try

            End If

        Next

    End Sub

答案 2 :(得分:0)

  
      
  • 找到所有的驱动器号   系统。
  •   
  • 每个驱动器
  •   
    •   
    • 如果特定文件夹存在则打开它。
    •   
  •   
  • 循环
  •   

答案 3 :(得分:0)

此链接包含一些基本内容。它应该让你指向正确的方向。此外,从代码示例中获取关键字并搜索MSDN。 MSN拥有大量文档和示例,可以帮助您进入下一步。

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6081470.html#

编辑 - 试试这个......

Imports System.IO

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each D As DriveInfo In DriveInfo.GetDrives
            If D.DriveType = DriveType.CDRom Then
                Debug.WriteLine(D.Name)
            End If

        Next
    End Sub
End Class

答案 4 :(得分:0)

您可以使用以下代码:

 Dim allDrives() As DriveInfo = DriveInfo.GetDrives()

    Dim d As DriveInfo
    For Each d In allDrives
        'Console.WriteLine("Drive {0}", d.Name)
        'Console.WriteLine("  Drive type: {0}", d.DriveType)
        If d.DriveType = DriveType.CDRom Then
            If d.IsReady = True Then
                Console.WriteLine("  Volume Name: {0}", d.Name)
                Console.WriteLine("  Volume label: {0}", d.VolumeLabel)
                Console.WriteLine("  File system: {0}", d.DriveFormat)
                Console.WriteLine( _
                    "  Available space to current user:{0, 15} bytes", _
                    d.AvailableFreeSpace)

                Console.WriteLine( _
                    "  Total available space:          {0, 15} bytes", _
                    d.TotalFreeSpace)

                Console.WriteLine( _
                    "  Total size of drive:            {0, 15} bytes ", _
                    d.TotalSize)
            End If
        End If

    Next