none
Ajuda Vb (pegar numero serie hd) RRS feed

  • Pergunta

  •  

    Sou muito iniciante em programaçao .....
    nao sei qual é a diferença de Visual Basic para .NET
    Estou usando "Microsoft Visual Basic 2005 Express Edition" , Ja achei varios codigos para ler o serial hd fisico alguns falam para criar modulos e adcionar outras coisas mais todos esses codigos quando eu coloko no form da erro no debug em baixo ... eu nao sei o que acontece

    exemplo

    1 - Passo: Adicione ao projeto Windows Form uma  Classe com o nome ClsDriveInfo e copie o código abaixo:



    """Codigo 

    Imports System

    Imports System.IO

    Imports System.Collections

    Imports System.Runtime.InteropServices

    Imports System.Text

    Public Class ClsDriveInfo

    Public Structure enCdRomInfo

    Public strDriveType As String, strLabel As String, strSn As String, strFileSystem As String

    Public fCapacity As String, fSpace As String

    End Structure

    Const DRIVE_CDROM As Integer = 5

    Const DRIVE_FIXED As Integer = 3

    Const DRIVE_NO_ROOT As Integer = 1

    Const DRIVE_REMOTE As Integer = 4

    Const DRIVE_REMOVABLE As Integer = 2

    <DLLIMPORT("kernel32")> _

    Private Shared Function GetDriveType(ByVal path As String) As Integer

    End Function

    <DLLIMPORT("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _

    Private Shared Function GetVolumeInformation(ByVal rootPathName As String, _

    ByVal volNameBuffer As StringBuilder, ByVal volNameSize As Integer, ByRef volSerialNumber As Integer, ByRef maxComponentLength As Integer, ByRef 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 lpFreeAvail As Int64, ByRef lpCapacity As Int64, ByRef lpFree As Int64) As Boolean

    End Function

     

    Private Shared Function ConvertBytes(ByVal nValue As Single) As String

    Dim strValue As String = ""

    If nValue > 1000000000 Then

    nValue = nValue / (1024 * 1024 * 1024)

    nValue = CSng(Math.Round(CDbl(nValue), 2))

    strValue = nValue.ToString() + " " + "GB"

    ElseIf nValue >= 1000000 AndAlso nValue <= 1000000000 Then

    nValue = nValue / (1024 * 1024)

    nValue = CSng(Math.Round(CDbl(nValue), 2))

    strValue = nValue.ToString() + " " + "MB"

    ElseIf nValue >= 1000 AndAlso nValue < 1000000 Then

    nValue = nValue / (1024)

    nValue = CSng(Math.Round(CDbl(nValue), 2))

    strValue = nValue.ToString() + " " + "KB"

    ElseIf nValue < 1000 Then

    nValue = CSng(Math.Round(CDbl(nValue), 2))

    strValue = nValue.ToString() + " " + "bytes"

    End If

    Return strValue

    End Function

    Public Shared Function EnumDrives() As String()

    Dim strDrives As String()

    strDrives = Directory.GetLogicalDrives()

    Return strDrives

    End Function

    Public Shared Function GetDriveInfo(ByVal strDrive As String) As enCdRomInfo

    Dim enInfo As New enCdRomInfo()

    Dim inf As New DirectoryInfo(strDrive)

    enInfo.strLabel = ""

    enInfo.strFileSystem = ""

    enInfo.strSn = ""

    enInfo.fCapacity = ""

    enInfo.fSpace = ""

    Dim volName As New StringBuilder(260)

    Dim fileSysName As New StringBuilder(260)

    Dim volSer As Integer, maxCmp As Integer, sysFlgs As Integer, err As Integer, driveType As Integer

    Dim nTotal As Int64, nFree As Int64, nAvail As Int64

    driveType = GetDriveType(strDrive)

    Select Case driveType

    Case DRIVE_CDROM

    enInfo.strDriveType = "CD-ROM"

    Exit Select

    Case DRIVE_FIXED

    enInfo.strDriveType = "HDD"

    Exit Select

    Case DRIVE_REMOTE

    enInfo.strDriveType = "Network Drive"

    Exit Select

    Case DRIVE_REMOVABLE

    enInfo.strDriveType = "Removable"

    Exit Select

    Case Else

    enInfo.strDriveType = "HDD"

    Exit Select

    End Select

    If inf.Exists Then

    Dim ok As Boolean = GetVolumeInformation(strDrive, volName, volName.Capacity, volSer, maxCmp, sysFlgs, _

    fileSysName, fileSysName.Capacity)

    If Not ok Then

    err = Marshal.GetLastWin32Error()

    End If

    ok = GetDiskFreeSpaceEx(strDrive, nAvail, nTotal, nFree)

    If Not ok Then

    err = Marshal.GetLastWin32Error()

    End If

    enInfo.strLabel = volName.ToString()

    enInfo.strFileSystem = fileSysName.ToString()

    enInfo.strSn = volSer.ToString()

    enInfo.fCapacity = ConvertBytes(nTotal)

    enInfo.fSpace = ConvertBytes(nFree)

    enInfo.strFileSystem = fileSysName.ToString()

    Else

    enInfo.strLabel = enInfo.strDriveType + " " + strDrive + " not present"

    End If

    Return enInfo

    End Function

    End Class

    Fim Codigo

     Nesse codigo ele manda Criar um Form e adicione o código (nao consegui xegar nessa parte porque nao passei da primeira)

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

    Dim DriveInfo As ClsDriveInfo.enCdRomInfo

    DriveInfo = ClsDriveInfo.GetDriveInfo("c:\")

    'exemplo:

    MsgBox(DriveInfo.strSn)

    MsgBox(DriveInfo.strLabel)

    MsgBox(DriveInfo.fCapacity)

    MsgBox(DriveInfo.fSpace)

    MsgBox(DriveInfo.strDriveType)

    End Sub

    Fim Codigo
    ** Para funcionar corretamente, incluir a "\" na letra da unidade. Exemplo: "C:\", "D:\"
    Agora vamos aos meus erros

    Adicionei a Classe ClsDriveInfo com o primeiro Codigo . Ficou no Solution Explorer como ClsDriveInfo.vb

    ai vai o link com as fotos do erro

    http://img117.imageshack.us/img117/7170/errovbxk7.jpg

     

    e eu sou muito novo com programaçao comecei fazendo uns programinhas de inicio em pascal e estou estudando sozinho intao ta dificel qualquer ajuda é bem vinda de tutorial pra iniciantes e livros e etc

    Grato a qualquer ajuda
    meu msn é powerguido13@hotmail.com

    segunda-feira, 12 de maio de 2008 20:18

Respostas

  •  

    Para pegar o número de serie pode usar esse código. So precisa de adicionar uma referência ao system.management

     

    Code Snippet

    Imports System.Management
     
    Public Class Form1
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim
    drives As New ArrayList
     
            Dim query As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
     
            For Each wmiDrive As ManagementObject In query.Get()
                Dim info As New DriveInfo
                info.Model = wmiDrive("Model").ToString()
                info.Type = wmiDrive("InterfaceType").ToString()
                
    drives.Add(info)
            Next
     
            query = New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")
     
            Dim i As Integer = 0
            For Each wmiDrive As ManagementObject In query.Get()
                ' Get the hard drive from collection using index.
                Dim info As DriveInfo = CType(
    drives(i), DriveInfo)
                ' Get the hardware serial number.
                If wmiDrive("SerialNumber") Is Nothing Then
                    info.SerialNumber = "None"
                Else
                    info.SerialNumber = wmiDrive("SerialNumber").ToString()
                End If
                i += 1
            Next
     
            ' Mostra a informação do disco
            For Each info As DriveInfo In
    drives
                Dim sBuilder As New System.Text.StringBuilder
                sBuilder.AppendLine("Model:" + info.Model)
                sBuilder.AppendLine("Type:" + info.Type)
                sBuilder.AppendLine("Serial No.:" + info.SerialNumber)
                MessageBox.Show(sBuilder.ToString)
            Next
     
        End Sub
    End Class
     
     
    Class DriveInfo
        Private _model As String
        Private _type As String
        Private _serialNumber As String
     
        Public Property Model() As String
            Get
                Return _model
            End Get
            Set(ByVal Value As String)
                _model = Value
            End Set
        End Property
     
        Public Property Type() As String
            Get
                Return _type
            End Get
            Set(ByVal Value As String)
                _type = Value
            End Set
        End Property
     
        Public Property SerialNumber() As String
            Get
                Return _serialNumber
            End Get
            Set(ByVal Value As String)
                _serialNumber = Value
            End Set
        End Property
     
    End Class

     

     

     

    segunda-feira, 12 de maio de 2008 22:41