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

发布于 2024-09-10 11:13:15 字数 141 浏览 7 评论 0原文

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

I'm trying to write a program that opens a folder from the CD disk when a button is clicked. The program will be run from a CD, and aims to open a certain folder. However, I can't use "shell "explorer...."" because the drive letter will change between different computers. Is there a way to open the folder straight from the CD in VB.NET

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

猛虎独行 2024-09-17 11:13:15

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

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

It is easy if you know that your program was started from the CD. Just read the program location back:

    Dim exePath As String = System.Reflection.Assembly.GetEntryAssembly().Location
    Dim drive As String = System.IO.Path.GetPathRoot(exePath)
吻泪 2024-09-17 11:13:15

例如,如果我想执行一个文件: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

Example, if I want execute an file: executable.exe;
In Optical drive E:\executables\executable.exe")

Code:

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
萝莉病 2024-09-17 11:13:15
  • 查找上的所有驱动器号
    系统。
  • 对于每个驱动器
    • 如果特定文件夹存在,请打开它。
  • 循环
  • Find all the drive letters on the
    system.
  • For each drive
    • if the specific folder exists open it.
  • Loop
煮茶煮酒煮时光 2024-09-17 11:13:15

该链接包含一些基本内容。它应该为您指明正确的方向。另外,从代码示例中获取关键字并搜索 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

This link contains some basic stuff. It should get you pointed in the right direction. Also, take keywords from the code sample and search MSDN. MSN has lots of documentation and samples that may get you to the next step.

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

edit - try this...

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
太阳男子 2024-09-17 11:13:15

您可以使用此代码:

 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

You can using this code:

 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文