Finding a Drive letter in vb.net

Finding a Drive letter in vb.net

GeeHaa
Collaborator Collaborator
4,289 Views
5 Replies
Message 1 of 6

Finding a Drive letter in vb.net

GeeHaa
Collaborator
Collaborator

I need to write a program to copy files from a CD to a hard drive. My problem is how can I determine what drive letter the CD is. Assuming the coping program is run from the CD.  It was easy with VBA I just embedded the macro in a drawing that was located on the CD.

 

Thanks in advance.

0 Likes
Accepted solutions (1)
4,290 Views
5 Replies
Replies (5)
Message 2 of 6

fieldguy
Advisor
Advisor

Soemthing like this (from http://www.a1vbcode.com/vbtip-55.asp)

Dim CDPath as String
Dim fso As New Scripting.FileSystemObject
Dim drv As Drive

For Each drv In fso.Drives
     If drv.DriveType = CDRom Then
          CDPath = drv.Path
          Exit For
     End If
Next drv
Set drv = Nothing
Set fso = Nothing
End Sub

 

0 Likes
Message 3 of 6

chiefbraincloud
Collaborator
Collaborator

If what you want to know is "Where is my program running from?" then you just use My.Application.Info.DirectoryPath.

 

If you need your program to seek out a cd or dvd drive, then the System.IO.DriveInfo class is what you need.  This is a very quick example that will return a list (of DriveInfo) for every Optical Drive found.

 

 Public Function FindCD() As List(Of IO.DriveInfo)
 FindCD = New List(Of IO.DriveInfo)
 Dim alldrives As ObjectModel.ReadOnlyCollection(Of IO.DriveInfo) = Drives
 For Each DrvInfo As IO.DriveInfo In alldrives
  If DrvInfo.DriveType = IO.DriveType.CDRom Then
   FindCD.Add(DrvInfo)
  End If
 Next
End Function

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 4 of 6

Hallex
Advisor
Advisor

MSDN is your best friend

Take a look

http://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives.aspx

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 5 of 6

Anonymous
Not applicable
Accepted solution

Hi there,

 

I personaly use this:

Dim _pad As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

This gets te location from the executing exe. Now you can trim you string. This even works when people copy the cd to there network drives.

 

Kind regards,

 

Irvin

0 Likes
Message 6 of 6

GeeHaa
Collaborator
Collaborator

That worked, only I needed the following Imports

 

Imports System.IO

Imports System.Reflection

 

 

Thank You all Very Much.

 

0 Likes