Reputation: 39
Here is my problem: I have make a program for myself that is managing the TV seria that I am watching. Yesterday I got the idea to add the possibility to copy all the seriafolder and the .exe file of the program to USB, so I can take it with me if i need it. This is solved. Now I need that the program recognize if it was started from an USB Key or from HDD. Considerate these points:
When is on an HDD the program is actually search for a locator file, that automatically will put the path wrote inside, to the textbox1.text. This should not be happen on USB, because it's a fixed path (SerVision\Telefilm) and only the drive letter could change. I was thinking to solve checking on the run if the textbox1.text is empty (because it can be the first time running from HDD or running from USB) in this way im my 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
Butin Textbox1, I always got only the drive name (from USB in mycase is G:) instead to have the full path (G.\SerVision\Telefilm).
This is my full 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
I've also tried to change it in this way:
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
Can someone please put me on the good way to solve this problem?
Upvotes: 0
Views: 64
Reputation: 2019
Actually there is a really easy way to do this with DriveInfo.DriveType
First just: Imports System.IO
Then using the function below you can just do:
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
Then just add the function bellow
Public Function GetDriveType(ByVal Drive As String) As DriveType
Dim MyDrive As New DriveInfo(Drive)
Return MyDrive.DriveType
End Function
Upvotes: 2