Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Elbrinner

Pegar o numero do disco rigido

Recommended Posts

Aqui tem o codigo q faz tudo isso e muito mais.

 

Imports SystemImports System.IOImports System.CollectionsImports System.Runtime.InteropServicesImports System.TextNamespace DriveInfo    Public Class CDriveInfo#Region " API Declarations "        <DllImport("kernel32")> _        Private Shared Function GetDriveType(ByVal Path As String) As Integer        End Function        <DllImport("kernel32.dll")> _        Private Shared Function GetVolumeInformation(ByVal RootPathName As String, ByVal VolNameBuffer As StringBuilder, ByVal VolumeSize As Integer, ByRef VolumeSerialNumber As Integer, ByRef maxComponentLength As Integer, ByVal FileSysFlags As Integer, ByVal FileSystemName As StringBuilder, ByVal FileSystemNameSize As Integer) As Boolean        End Function        <DllImport("kernel32")> _        Private Shared Function GetDiskFreeSpaceEx(ByVal Path As String, ByRef Available As Int64, ByRef Capacity As Int64, ByRef Free As Int64) As Boolean        End Function#End Region        Private m_colDrives As New Collections.ArrayList()        Public Sub New()            'Create an array to store the drives temporarily            Dim sDrives() As String            'Create a temp drive            Dim tDrive As CDrive            'Get the list of logical drives            sDrives = EnumDrives()            Dim iNum As Integer            For iNum = 0 To sDrives.GetUpperBound(0)                'pull the information about the drive                tDrive = GetDriveInfo(sDrives(iNum))                'add the drive to the collection, so that they                'can be accessed later                m_colDrives.Add(tDrive)            Next        End Sub        'Returns an array of all the logical drives on a computer        Public Shared Function EnumDrives() As String()            Return Directory.GetLogicalDrives()        End Function        'Custom function to obtain all the required information about a drive        'and return it as a CDrive object        Public Shared Function GetDriveInfo(ByVal DriveLetter As String) As CDrive            'Create a temp drive, this will be return to the calling function            Dim tDrive As CDrive            'Create an object with basic directory information            Dim inf As New DirectoryInfo(DriveLetter)            'Don't remember exactly why, but these two were easier with the Stringbuilder            Dim sVolName As New StringBuilder("")            Dim sFileSysName As New StringBuilder("")            Dim iVolSer, iMaxCmp, iSysFlags, iErr As Integer            Dim iDriveType As Integer            Dim nTotal, nFree, nAvailable As Int64            'Determine the Drive type (Fixed, CDRom, Remote, etc...)            iDriveType = GetDriveType(DriveLetter)            'Check to see if the drive exist (readable)            'Useful for if you don't have a floppy disk in the drive.            If inf.Exists Then                'Return the information about the drive from the system                Dim bOK As Boolean = GetVolumeInformation(DriveLetter, sVolName, sVolName.Capacity, iVolSer, iMaxCmp, iSysFlags, sFileSysName, sFileSysName.Capacity)                If Not bOK Then                    iErr = Marshal.GetLastWin32Error                End If                'Return the disk free space                bOK = GetDiskFreeSpaceEx(DriveLetter, nAvailable, nTotal, nFree)                If Not bOK Then                    iErr = Marshal.GetLastWin32Error                End If                'Create a CDrive object by passing in all of the information that we have gathered about the particular drive                tDrive = New CDrive(DriveLetter, sVolName.ToString, iVolSer.ToString, sFileSysName.ToString, iDriveType, nTotal, nFree)            Else                'Create a blank CDrive object with only the Drive letter                tDrive = New CDrive(DriveLetter)            End If            'Return the CDrive to the calling function            Return tDrive        End Function        'This is the readonly property to access the collection of information        'that was filled when you instantiated your CDriveInfo object        Public ReadOnly Property Drives() As Collections.ArrayList            Get                Return m_colDrives            End Get        End Property    End Class    'This is the class that holds the information for a particular drive    Public Class CDrive        Public Enum eDriveTypes            No_Root = 1            Removable = 2            Fixed = 3            Remote = 4            CDRom = 5        End Enum        'Created this so that you could see the text representation (2.5GB) or the        'numberic representation (1222000)        Public Structure enSpace            Public Number As Double            Public Text As String        End Structure        Private m_sDriveType As eDriveTypes        Private m_sLabel As String = ""        Private m_sSerialNumber As String = ""        Private m_sFileSystem As String = ""        Private m_sDriveLetter As String = ""        Private m_dCapacity As New enSpace()        Private m_dFreeSpace As New enSpace()        Public Sub New(ByVal Letter As String, ByVal sLabel As String, ByVal sSerialNumber As String, ByVal sFileSystem As String, ByVal sDriveType As eDriveTypes, ByVal dCapacity As Double, ByVal dFreeSpace As Double)            m_sDriveLetter = Letter            m_sLabel = sLabel            m_sSerialNumber = sSerialNumber            m_sFileSystem = sFileSystem            m_dCapacity.Number = dCapacity            m_dCapacity.Text = ConvertFreeBytes(dCapacity)            m_dFreeSpace.Number = dFreeSpace            m_dFreeSpace.Text = ConvertFreeBytes(dFreeSpace)            m_sDriveType = sDriveType        End Sub        'This is the Constructor that is called if the drive does not exist. (Can not read from the drive)        Public Sub New(ByVal Letter As String)            m_sDriveLetter = Letter            m_sLabel = "Drive is not present"            m_dCapacity.Number = 0            m_dCapacity.Text = ConvertFreeBytes(0)            m_dFreeSpace.Number = 0            m_dFreeSpace.Text = ConvertFreeBytes(0)        End Sub        Public ReadOnly Property DriveLetter() As String            Get                Return m_sDriveLetter            End Get        End Property        Public ReadOnly Property DriveType() As eDriveTypes            Get                Return m_sDriveType            End Get        End Property        Public ReadOnly Property Label() As String            Get                Return m_sLabel            End Get        End Property        Public ReadOnly Property SerialNumber() As String            Get                Return m_sSerialNumber            End Get        End Property        Public ReadOnly Property FileSystem() As String            Get                Return m_sFileSystem            End Get        End Property        Public ReadOnly Property TotalSpace() As enSpace            Get                Return m_dCapacity            End Get        End Property        Public ReadOnly Property FreeSpace() As enSpace            Get                Return m_dFreeSpace            End Get        End Property        'Converts the byte count to our text representation        Private Function ConvertFreeBytes(ByVal Value As Double) As String            Dim sValue As String = ""            If Value > (1024 ^ 4) Then                Value /= (1024 ^ 4)                Value = Math.Round(Value, 2)                Return Value.ToString & " TB"            ElseIf Value > (1024 ^ 3) And Value < (1024 ^ 4) Then                Value /= (1024 ^ 3)                Value = Math.Round(Value, 2)                Return Value.ToString & " GB"            ElseIf Value > (1024 ^ 2) And Value < (1024 ^ 3) Then                Value /= (1024 ^ 2)                Value = Math.Round(Value, 2)                Return Value.ToString & " MB"            ElseIf Value > 1024 And Value < (1024 ^ 2) Then                Value /= 1024                Value = Math.Round(Value, 2)                Return Value.ToString & " KB"            ElseIf Value < 1024 Then                Value = Math.Round(Value, 2)                Return Value.ToString & " bytes"            End If        End Function    End ClassEnd Namespace

 

Para motrar

 

Public Class frmMain    Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code "    Public Sub New()        MyBase.New()        'This call is required by the Windows Form Designer.        InitializeComponent()        'Add any initialization after the InitializeComponent() call    End Sub    'Form overrides dispose to clean up the component list.    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)        If disposing Then            If Not (components Is Nothing) Then                components.Dispose()            End If        End If        MyBase.Dispose(disposing)    End Sub    'Required by the Windows Form Designer    Private components As System.ComponentModel.IContainer    'NOTE: The following procedure is required by the Windows Form Designer    'It can be modified using the Windows Form Designer.      'Do not modify it using the code editor.    Friend WithEvents trvDrives As System.Windows.Forms.TreeView    Friend WithEvents mnuMain As System.Windows.Forms.MainMenu    Friend WithEvents mnuFile As System.Windows.Forms.MenuItem    Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem    Friend WithEvents mnuLoad As System.Windows.Forms.MenuItem    Friend WithEvents mnuLoadIndividual As System.Windows.Forms.MenuItem    Friend WithEvents mnuLoadDriveInfo As System.Windows.Forms.MenuItem    Friend WithEvents mnuView As System.Windows.Forms.MenuItem    Friend WithEvents mnuViewCollapse As System.Windows.Forms.MenuItem    Friend WithEvents mnuViewExpand As System.Windows.Forms.MenuItem    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()        Me.trvDrives = New System.Windows.Forms.TreeView        Me.mnuMain = New System.Windows.Forms.MainMenu        Me.mnuFile = New System.Windows.Forms.MenuItem        Me.mnuFileExit = New System.Windows.Forms.MenuItem        Me.mnuLoad = New System.Windows.Forms.MenuItem        Me.mnuLoadIndividual = New System.Windows.Forms.MenuItem        Me.mnuLoadDriveInfo = New System.Windows.Forms.MenuItem        Me.mnuView = New System.Windows.Forms.MenuItem        Me.mnuViewCollapse = New System.Windows.Forms.MenuItem        Me.mnuViewExpand = New System.Windows.Forms.MenuItem        Me.SuspendLayout()        '        'trvDrives        '        Me.trvDrives.Dock = System.Windows.Forms.DockStyle.Fill        Me.trvDrives.ImageIndex = -1        Me.trvDrives.Location = New System.Drawing.Point(0, 0)        Me.trvDrives.Name = "trvDrives"        Me.trvDrives.SelectedImageIndex = -1        Me.trvDrives.Size = New System.Drawing.Size(272, 421)        Me.trvDrives.TabIndex = 0        '        'mnuMain        '        Me.mnuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuLoad, Me.mnuView})        '        'mnuFile        '        Me.mnuFile.Index = 0        Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileExit})        Me.mnuFile.Text = "&File"        '        'mnuFileExit        '        Me.mnuFileExit.Index = 0        Me.mnuFileExit.Text = "E&xit"        '        'mnuLoad        '        Me.mnuLoad.Index = 1        Me.mnuLoad.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuLoadIndividual, Me.mnuLoadDriveInfo})        Me.mnuLoad.Text = "&Load"        '        'mnuLoadIndividual        '        Me.mnuLoadIndividual.Index = 0        Me.mnuLoadIndividual.Text = "&Individual Drives"        '        'mnuLoadDriveInfo        '        Me.mnuLoadDriveInfo.Index = 1        Me.mnuLoadDriveInfo.Text = "&From CDriveInfo Class"        '        'mnuView        '        Me.mnuView.Enabled = False        Me.mnuView.Index = 2        Me.mnuView.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuViewCollapse, Me.mnuViewExpand})        Me.mnuView.Text = "&View"        '        'mnuViewCollapse        '        Me.mnuViewCollapse.Index = 0        Me.mnuViewCollapse.Text = "&Collapse"        '        'mnuViewExpand        '        Me.mnuViewExpand.Index = 1        Me.mnuViewExpand.Text = "&Expand"        '        'frmMain        '        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)        Me.ClientSize = New System.Drawing.Size(272, 421)        Me.Controls.Add(Me.trvDrives)        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))        Me.Menu = Me.mnuMain        Me.Name = "frmMain"        Me.Text = "CDriveInfo Viewer"        Me.ResumeLayout(False)    End Sub#End Region    Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click        Me.Close()    End Sub    Private Sub mnuLoadIndividual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLoadIndividual.Click        'Clear the contents of the treeview so that we do not have double information        trvDrives.Nodes.Clear()        Dim iNum As Integer        'Create a string array to hold all of the drive letters        Dim sDrives() As String        'Create a CDrive object that we'll use to get the actual drive information        Dim myInfo As DriveInfo.CDrive        'Load the drives into the array from the shared method EnumDrives        sDrives = DriveInfo.CDriveInfo.EnumDrives        For iNum = 0 To sDrives.GetUpperBound(0)            'Load the information via the shared GetDriveInfo method            myInfo = DriveInfo.CDriveInfo.GetDriveInfo(sDrives(iNum))            'For the purpose of the example, add the information to the tree view            AddToTree(myInfo, iNum)        Next        mnuView.Enabled = True    End Sub    Private Sub mnuLoadDriveInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuLoadDriveInfo.Click        'Clear the contents of the treeview so that we do not have double information        trvDrives.Nodes.Clear()        Dim iNum As Integer        'Create a CDriveInfo object        'This will contain all of the drives information automatically, when        'you instantiate it        Dim myDrives As DriveInfo.CDriveInfo        myDrives = New DriveInfo.CDriveInfo()        'Create a drive to hold the information from the Collection of drives        'stored in myDrives        Dim myInfo As DriveInfo.CDrive        'Loop through each drive in the collection        For Each myInfo In myDrives.Drives            'You can pull information directly from the drive here            Debug.WriteLine(myInfo.FreeSpace.Text.ToString)            'For the purpose of the example, add the information to the tree view            AddToTree(myInfo, iNum)            iNum += 1        Next        mnuView.Enabled = True    End Sub    Private Sub mnuViewCollapse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewCollapse.Click        trvDrives.BeginUpdate()        trvDrives.CollapseAll()        trvDrives.EndUpdate()    End Sub    Private Sub mnuViewExpand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuViewExpand.Click        trvDrives.BeginUpdate()        trvDrives.ExpandAll()        trvDrives.EndUpdate()    End Sub    Private Sub AddToTree(ByVal DriveInformation As DriveInfo.CDrive, ByVal Position As Int32)        'This method is here just so that we can view the information that we have pulled.        trvDrives.BeginUpdate()        trvDrives.Nodes.Add(New TreeNode(DriveInformation.DriveLetter.ToString))        trvDrives.Nodes(Position).Nodes.Add("Drive Type: " & DriveInformation.DriveType.ToString)        trvDrives.Nodes(Position).Nodes.Add("File System: " & DriveInformation.FileSystem.ToString)        trvDrives.Nodes(Position).Nodes.Add("Drive Label: " & DriveInformation.Label.ToString)        trvDrives.Nodes(Position).Nodes.Add("Total Space: " & DriveInformation.TotalSpace.Text.ToString)        trvDrives.Nodes(Position).Nodes.Add("Free Space: " & DriveInformation.FreeSpace.Text.ToString)        trvDrives.Nodes(Position).Nodes.Add("Serial Number: " & DriveInformation.SerialNumber.ToString)        trvDrives.EndUpdate()    End Sub

 

t+

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.