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
Asked
Active
Viewed 2,124 times
5 Answers
2
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)

Hans Passant
- 922,412
- 146
- 1,693
- 2,536
-
Does the 'drive' variable refer to the executed path or to the drive letter? – shmuxel Jul 17 '10 at 20:53
-
If the CD drive is D then drive will contain "D:\". Use Path.Combine to create the full path to the directory name. – Hans Passant Jul 17 '10 at 21:00
1
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

Robert Moskal
- 21,737
- 8
- 62
- 86

Paulo Barbosa
- 11
- 1
0
- Find all the drive letters on the system.
- For each drive
- if the specific folder exists open it.
- Loop

johnjohn
- 4,221
- 7
- 36
- 46
-
Right, thanks for that, but can you provide the specific syntax for this. Thanks :) – shmuxel Jul 17 '10 at 17:48
0
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

Community
- 1
- 1

DenaliHardtail
- 27,362
- 56
- 154
- 233
0
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

Sour LeangChhean
- 7,089
- 6
- 37
- 39