如何知道程序是从 USB 还是固定硬盘运行?

How to know if the program is runnig from USB or Fixed HDD?

这是我的问题:我为自己制作了一个程序来管理我正在观看的电视剧。昨天我想到了添加将程序的所有 seriafolder 和 .exe 文件复制到 USB 的可能性,这样我可以在需要时随身携带。这就解决了。现在我需要程序识别它是从 USB Key 还是从 HDD 启动的。考虑以下几点:

  1. 当在硬盘上时,程序实际上是搜索一个定位器文件,自动将写入的路径放在里面,到textbox1.text。这不应该发生在 USB 上,因为它是固定路径 (SerVision\Telefilm),只有驱动器号可以更改。如果 textbox1.text 为空(因为这可能是第一次从 HDD 运行ning 或从 USB 运行ning ),我想解决检查 运行 的问题我的方式是 form1_load:

    Dim myd As DriveInfo
    For Each myd In DriveInfo.GetDrives
        If TextBox1.Text = "" AndAlso myd.IsReady AndAlso myd.DriveType = IO.DriveType.Removable Then
            Dim USBPath As String = myd.Name + "Servision\Telefilm\"
            Call AggiornaListView()
        End If
        If TextBox1.Text = "" AndAlso myd.IsReady AndAlso myd.DriveType = IO.DriveType.Fixed Then
            Dim ROAD As String = Application.StartupPath()
            TextBox1.Text = ROAD
        End If
    Next
    

但是在 Textbox1 中,我总是只得到驱动器名称(在我的情况下来自 USB 是 G:) 而不是完整路径(G.\SerVision\Telefilm)。

  1. 这是我的全form1_load:

    Try 
        ListBox1.Enabled = False          
        TextBox3.Visible = False
        Button3.Enabled = False
        Dim Path As String = Application.StartupPath() + "\SVlocator.loc"
    
        If File.Exists(Path) = False Then
    
            Dim sw As StreamWriter = New StreamWriter(Path)
    
            sw.WriteLine(TextBox1.Text)
            sw.Close()
    
    
            Call AggiornaListView()
        Else
    
            Dim sr As StreamReader = New StreamReader(Path)
    
            'This allows you to do one Read operation.
    
            TextBox1.Text = (sr.ReadToEnd())
            sr.Close()
    
    
        End If
    
    
        Dim myd As DriveInfo
    
        For Each myd In DriveInfo.GetDrives
            If TextBox1.Text = "" AndAlso myd.IsReady AndAlso myd.DriveType = IO.DriveType.Removable Then
                Dim USBPath As String = myd.Name + "Servision\Telefilm\"
                Call AggiornaListView()
            End If
            If TextBox1.Text = "" AndAlso myd.IsReady AndAlso myd.DriveType = IO.DriveType.Fixed Then
                Dim ROAD As String = Application.StartupPath()
                TextBox1.Text = ROAD
            End If
        Next
    
    
        Call AggiornaListView()
    
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    
  2. 我也试过改成这样:

    Try 
        ListBox1.Enabled = False
        TextBox3.Visible = False
        Button3.Enabled = False
    
        Dim Path As String = Application.StartupPath() + "\SVlocator.loc"
    
        If File.Exists(Path) = False Then
    
            Dim myd As DriveInfo
    
            For Each myd In DriveInfo.GetDrives
                If TextBox1.Text = "" AndAlso myd.IsReady AndAlso myd.DriveType = IO.DriveType.Removable Then
                    Dim USBPath As String = myd.Name + "Servision\Telefilm\"
                    Call AggiornaListView()
                End If
            Next
            If TextBox1.Text = "" AndAlso myd.IsReady AndAlso myd.DriveType = IO.DriveType.Fixed Then
                    Dim ROAD As String = Application.StartupPath()
                TextBox1.Text = ROAD
                Call AggiornaListView()
            End If
    
    
    
            Call AggiornaListView()
    
            Dim sw As StreamWriter = New StreamWriter(Path)
    
            sw.WriteLine(TextBox1.Text)
            sw.Close()
    
    
            Call AggiornaListView()
        Else
    
            Dim sr As StreamReader = New StreamReader(Path)
    
            'This allows you to do one Read operation.
    
            TextBox1.Text = (sr.ReadToEnd())
            sr.Close()
    
    
        End If
    
    
        Call AggiornaListView()
    
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    

有人可以告诉我解决这个问题的好方法吗?

实际上有一个非常简单的方法可以做到这一点 DriveInfo.DriveType

先刚:Imports System.IO

然后使用下面的函数就可以了:

Dim MyDrive As String = Path.GetPathRoot(Application.StartupPath)

If GetDriveType(MyDrive) = DriveType.Removable Then
    'Program running from USB
Else
    'Program running from PC
End If

然后只需添加以下功能

Public Function GetDriveType(ByVal Drive As String) As DriveType
    Dim MyDrive As New DriveInfo(Drive)
    Return MyDrive.DriveType
End Function