0

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

4

5 回答 5

2

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

    Dim exePath As String = System.Reflection.Assembly.GetEntryAssembly().Location
    Dim drive As String = System.IO.Path.GetPathRoot(exePath)
于 2010-07-17T18:36:31.800 回答
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
于 2018-11-01T23:29:58.190 回答
0
  • 查找系统上的所有驱动器号。
  • 对于每个驱动器
    • 如果存在特定文件夹,请打开它。
  • 环形
于 2010-07-17T17:46:27.727 回答
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
于 2010-07-17T17:54:11.660 回答
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
于 2018-04-20T09:28:32.060 回答