determining drive types

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I program in C#. Is there a way to determine which of the local machine's
logical drives are associated with CD drive(s)?
 
Hi Greg,

Try this:

Dim drive As driveinfo
Dim aldrives As New ArrayList

drive = New driveinfo

aldrives.Add(drive)

The arraylist should not have the drive.description of drive for each drive
in your pc.

HTH,

Bernie Yaeger
 
Hi Again Greg,

Oops - gave you vb .net code in my other reply - but you should be able to
convert that easily.

Bernie
 
Thanks Bernie,

I'm trying to design code that will work on a user's machine when they run
my program. I'm not sure I understand how this would help in that situation.
I appreciate your feedback, am I missing something in interpreting what you
wrote?
 
Hi Greg,

Here's the vb .net code again, but my explanation used 'not' when it should
have said 'now':

Dim drive As driveinfo
Dim aldrives As New ArrayList

drive = New driveinfo

aldrives.Add(drive)

The arraylist should NOW have the drive.description of drive for each drive
in your pc. The drive.description will give you 'cd... etc'

Bernie
 
Hi Bernie,

Where is the drive info coming from that's being added to the array list?
In other words how is "driveinfo" receiving the information? What is
"driveinfo"? I cannot find any references to it in the .NET help files. I'm
sorry, but you lost me with "driveinfo".
 
Hi Greg,

I'm sorry for confusing you; I've not given you all the code, but once I
explain, I think this will help you.

I am currently reading 'developing .net controls with vb .net'. One of the
chapters deals with placing icons and names of drives in a selection
combobox - I've changed it to a listbox, but it works fine. One of the
things it does, is query the pc for all of its drives and then assigns
descriptions and icons that match - cd rom drive, c drive, a floppy, etc.
This is exactly the kind of info I believe you could use. So create a form
called 'aownerdraw', add a listbox called 'cbdrives', set its drawmode to
'ownerdrawfixed' and use this complete code (driveinfo is a class that is
created at the bottom of this code sample). This should give you the info
you need to roll your own code to meet your needs.

Imports System.Collections

Public Class aownerdraw

Inherits System.Windows.Forms.Form

Dim drive As driveinfo

Dim aldrives As New ArrayList

Dim mgtobjectsearcher As System.Management.ManagementObjectSearcher

Dim mgtobject As System.Management.ManagementObject

+windows form designer generated code goes here

Private Sub aownerdraw_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

mgtobjectsearcher = New System.Management.ManagementObjectSearcher("select *
from win32_logicaldisk")

drive = New driveinfo

With drive

..drivedescription = "Desktop"

..driveletter = "Desktop"

..driveletter = 0

End With

aldrives.Add(drive)

Call updatedrives()

Call adddrivestocombo()

End Sub

Private Sub updatedrives()

For Each mgtobject In mgtobjectsearcher.Get()

drive = New driveinfo

Dim sdrivedescription As String = ""

sdrivedescription = mgtobject.Properties("DeviceID").Value

drive.driveletter = sdrivedescription

sdrivedescription &= "\ " & mgtobject.Properties("Description").Value

If IsNothing(mgtobject.Properties("Size").Value) Then

sdrivedescription &= " Not Available"

Else

sdrivedescription &= " " & mgtobject.Properties("VolumeName").Value

End If

drive.drivedescription = sdrivedescription

Select Case Convert.ToInt16(mgtobject.Properties("DriveType").Value)

Case 0 : drive.driveimage = 6

Case 1 : drive.driveimage = 6

Case 2 : drive.driveimage = 1

Case 3 : drive.driveimage = 2

Case 4 : drive.driveimage = 4

Case 5 : drive.driveimage = 3

Case 6 : drive.driveimage = 0

End Select

If (drive.driveimage = 4) Then

If sdrivedescription.IndexOfAny("Not Available") > 0 Then

drive.driveimage = 5

End If

End If

aldrives.Add(drive)

Next

End Sub

Private Sub adddrivestocombo()

Dim ddrive As driveinfo

Dim i As Integer

For Each ddrive In aldrives

cbdrives.Items.Add("")

Next

If aldrives.Count > 0 Then

cbdrives.SelectedIndex = 0

End If

End Sub

Private Sub cbdrives_DrawItem(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles cbdrives.DrawItem

e.DrawBackground()

e.DrawFocusRectangle()

If e.Index = 0 Then

Exit Sub

End If

Dim ioffset As Integer = 0

If e.Index < 0 Then

ioffset = 0

Else

ioffset = 10

End If

Dim ddrive As New driveinfo

ddrive = aldrives(e.Index)

Dim sdrivedescription As String = ddrive.drivedescription

Dim idriveimage As Integer = ddrive.driveimage

Dim bmdrivebmp As Bitmap = ildrives.Images(idriveimage)

Dim abrush As Brush = System.Drawing.Brushes.Black

Dim sformat As StringFormat = StringFormat.GenericTypographic

Dim itemheight As Integer = cbdrives.ItemHeight

e.DrawBackground()

e.DrawFocusRectangle()

e.Graphics.DrawImage(bmdrivebmp, ioffset, e.Bounds.Top + (itemheight -
bmdrivebmp.Height) \ 2)

If (e.State And DrawItemState.Selected) Then

abrush = System.Drawing.Brushes.White

End If

sformat.LineAlignment = StringAlignment.Center

e.Graphics.DrawString(sdrivedescription, e.Font, abrush, (20 + ioffset),
e.Bounds.Top + (e.Bounds.Height \ 2), sformat)

End Sub

End Class

Friend Class driveinfo

Dim m_driveletter As String = ""

Dim m_drivedescription As String

Dim m_driveimage As Integer = 0

Sub New()

End Sub

Friend Property driveletter() As String

Get

Return m_driveletter

End Get

Set(ByVal Value As String)

m_driveletter = Value

End Set

End Property

Friend Property drivedescription() As String

Get

Return m_drivedescription

End Get

Set(ByVal Value As String)

m_drivedescription = Value

End Set

End Property

Friend Property driveimage() As Integer

Get

Return m_driveimage

End Get

Set(ByVal Value As Integer)

m_driveimage = Value

End Set

End Property

End Class

HTH,

Bernie
 
Thanks Bernie,

it's much clearer now.

Bernie Yaeger said:
Hi Greg,

I'm sorry for confusing you; I've not given you all the code, but once I
explain, I think this will help you.

I am currently reading 'developing .net controls with vb .net'. One of the
chapters deals with placing icons and names of drives in a selection
combobox - I've changed it to a listbox, but it works fine. One of the
things it does, is query the pc for all of its drives and then assigns
descriptions and icons that match - cd rom drive, c drive, a floppy, etc.
This is exactly the kind of info I believe you could use. So create a form
called 'aownerdraw', add a listbox called 'cbdrives', set its drawmode to
'ownerdrawfixed' and use this complete code (driveinfo is a class that is
created at the bottom of this code sample). This should give you the info
you need to roll your own code to meet your needs.

Imports System.Collections

Public Class aownerdraw

Inherits System.Windows.Forms.Form

Dim drive As driveinfo

Dim aldrives As New ArrayList

Dim mgtobjectsearcher As System.Management.ManagementObjectSearcher

Dim mgtobject As System.Management.ManagementObject

+windows form designer generated code goes here

Private Sub aownerdraw_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

mgtobjectsearcher = New System.Management.ManagementObjectSearcher("select *
from win32_logicaldisk")

drive = New driveinfo

With drive

..drivedescription = "Desktop"

..driveletter = "Desktop"

..driveletter = 0

End With

aldrives.Add(drive)

Call updatedrives()

Call adddrivestocombo()

End Sub

Private Sub updatedrives()

For Each mgtobject In mgtobjectsearcher.Get()

drive = New driveinfo

Dim sdrivedescription As String = ""

sdrivedescription = mgtobject.Properties("DeviceID").Value

drive.driveletter = sdrivedescription

sdrivedescription &= "\ " & mgtobject.Properties("Description").Value

If IsNothing(mgtobject.Properties("Size").Value) Then

sdrivedescription &= " Not Available"

Else

sdrivedescription &= " " & mgtobject.Properties("VolumeName").Value

End If

drive.drivedescription = sdrivedescription

Select Case Convert.ToInt16(mgtobject.Properties("DriveType").Value)

Case 0 : drive.driveimage = 6

Case 1 : drive.driveimage = 6

Case 2 : drive.driveimage = 1

Case 3 : drive.driveimage = 2

Case 4 : drive.driveimage = 4

Case 5 : drive.driveimage = 3

Case 6 : drive.driveimage = 0

End Select

If (drive.driveimage = 4) Then

If sdrivedescription.IndexOfAny("Not Available") > 0 Then

drive.driveimage = 5

End If

End If

aldrives.Add(drive)

Next

End Sub

Private Sub adddrivestocombo()

Dim ddrive As driveinfo

Dim i As Integer

For Each ddrive In aldrives

cbdrives.Items.Add("")

Next

If aldrives.Count > 0 Then

cbdrives.SelectedIndex = 0

End If

End Sub

Private Sub cbdrives_DrawItem(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles cbdrives.DrawItem

e.DrawBackground()

e.DrawFocusRectangle()

If e.Index = 0 Then

Exit Sub

End If

Dim ioffset As Integer = 0

If e.Index < 0 Then

ioffset = 0

Else

ioffset = 10

End If

Dim ddrive As New driveinfo

ddrive = aldrives(e.Index)

Dim sdrivedescription As String = ddrive.drivedescription

Dim idriveimage As Integer = ddrive.driveimage

Dim bmdrivebmp As Bitmap = ildrives.Images(idriveimage)

Dim abrush As Brush = System.Drawing.Brushes.Black

Dim sformat As StringFormat = StringFormat.GenericTypographic

Dim itemheight As Integer = cbdrives.ItemHeight

e.DrawBackground()

e.DrawFocusRectangle()

e.Graphics.DrawImage(bmdrivebmp, ioffset, e.Bounds.Top + (itemheight -
bmdrivebmp.Height) \ 2)

If (e.State And DrawItemState.Selected) Then

abrush = System.Drawing.Brushes.White

End If

sformat.LineAlignment = StringAlignment.Center

e.Graphics.DrawString(sdrivedescription, e.Font, abrush, (20 + ioffset),
e.Bounds.Top + (e.Bounds.Height \ 2), sformat)

End Sub

End Class

Friend Class driveinfo

Dim m_driveletter As String = ""

Dim m_drivedescription As String

Dim m_driveimage As Integer = 0

Sub New()

End Sub

Friend Property driveletter() As String

Get

Return m_driveletter

End Get

Set(ByVal Value As String)

m_driveletter = Value

End Set

End Property

Friend Property drivedescription() As String

Get

Return m_drivedescription

End Get

Set(ByVal Value As String)

m_drivedescription = Value

End Set

End Property

Friend Property driveimage() As Integer

Get

Return m_driveimage

End Get

Set(ByVal Value As Integer)

m_driveimage = Value

End Set

End Property

End Class

HTH,

Bernie
 
Back
Top