2017-04-19 60 views
0

這是我的問題:我爲自己製作了一個管理正在觀看的電視劇的節目。昨天我的想法是增加將程序的所有seria文件夾和.exe文件複製到USB的可能性,所以我可以隨身攜帶,如果我需要的話。這解決了。現在我需要程序識別它是從USB密鑰還是從硬盤啓動。體貼以下幾點:如何知道程序是從USB還是固定硬盤運行?

  1. 當是放到HDD程序實際上是搜索定位文件,自動就會把路徑寫到裏面,到textbox1.text。這不應該發生在USB上,因爲它是一條固定路徑(SerVision \ Telefilm),只有驅動器號才能改變。我想,解決在運行檢查是否textbox1.text是空的(因爲它可以在第一時間從硬盤運行或USB運行)以這種方式即時通訊我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)。

  • 這是我的全部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 
    
  • 我也試着去改變它以這樣的方式

    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 
    
  • 有人可以請我解決這個問題的好方法嗎?

    回答

    1

    其實有一個非常簡單的方法與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 
    
    +1

    不錯,但我有兩個注意事項: ** A)**'退出選擇'是沒有必要的,因爲在同一種情況下沒有代碼_ ** AND ** _,因爲'Return'已經退出整個函數。 ** B)**您的整個'Select Case'語句可以縮短爲:'Return driveType'。 –

    +0

    只有當您想要提前退出當前案例時(例如,如果您檢查條件並且希望跳過案例中的其餘代碼),纔會使用「退出選擇」。 –

    +0

    @VisualVincent這是真的,因爲他不需要檢查和處理每一種類型,我們可以直接返回** DriveType **就像編輯好的代碼一樣嗎? – Mederic

    相關問題