Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. vb6 to vb.net help needed

vb6 to vb.net help needed

Scheduled Pinned Locked Moved Visual Basic
csharpwinformsgraphicshelp
16 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    offroaderdan
    wrote on last edited by
    #1

    hi there, i asked a question earlier to try and get information from command prompt howver i seem to have found a better way. here is the code used

    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports System.Management

    Namespace VBDotNet_WMI_HD_GetSerialNumber_2

    Public Class MainForm
        Inherits System.Windows.Forms.Form
        Private button1 As System.Windows.Forms.Button
    
        Public Shared Sub Main()
            Dim fMainForm As New MainForm
            fMainForm.ShowDialog()
        End Sub
    
        Public Sub New()
            MyBase.New()
            Me.InitializeComponent()
        End Sub
    

    #Region " Windows Forms Designer generated code "
    Private Sub InitializeComponent()
    Me.button1 = New System.Windows.Forms.Button
    Me.SuspendLayout()
    '
    'button1
    '
    Me.button1.Location = New System.Drawing.Point(8, 8)
    Me.button1.Name = "button1"
    Me.button1.Size = New System.Drawing.Size(280, 32)
    Me.button1.TabIndex = 0
    Me.button1.Text = "&Get serial number"
    AddHandler Me.button1.Click, AddressOf Me.Button1Click
    '
    'MainForm
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 45)
    Me.Controls.Add(Me.button1)
    Me.Name = "MainForm"
    Me.Text = "Testform <WMI - Harddisk - Get Serial Number. 2>"
    Me.ResumeLayout(False)
    End Sub
    #End Region

        Private Sub Button1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim Result As String = GetHarddiskSerialNumber("c:\\")
            If (Not Result Is String.Empty) Then
                MessageBox.Show(Result, "Info")
            End If
        End Sub
    
        Private Function GetHarddiskSerialNumber(ByVal drv As String) As String
            Try
                Dim BackSlash As String = "\\"
                Dim Position As Integer = drv.LastIndexOf(BackSlash)
    
                If Position > -1 Then
                    drv = drv.Substring(0, drv.Length - 1)
                Else
                    drv = drv
                End If
                Dim mo As New \_
                 ManagementObject("Win32\_LogicalDisk.DeviceID=""" & drv & """")
                Dim pd As PropertyData = mo.Properties("VolumeSerialNumber")
    
    D L 2 Replies Last reply
    0
    • O offroaderdan

      hi there, i asked a question earlier to try and get information from command prompt howver i seem to have found a better way. here is the code used

      Imports System
      Imports System.Drawing
      Imports System.Windows.Forms
      Imports System.Management

      Namespace VBDotNet_WMI_HD_GetSerialNumber_2

      Public Class MainForm
          Inherits System.Windows.Forms.Form
          Private button1 As System.Windows.Forms.Button
      
          Public Shared Sub Main()
              Dim fMainForm As New MainForm
              fMainForm.ShowDialog()
          End Sub
      
          Public Sub New()
              MyBase.New()
              Me.InitializeComponent()
          End Sub
      

      #Region " Windows Forms Designer generated code "
      Private Sub InitializeComponent()
      Me.button1 = New System.Windows.Forms.Button
      Me.SuspendLayout()
      '
      'button1
      '
      Me.button1.Location = New System.Drawing.Point(8, 8)
      Me.button1.Name = "button1"
      Me.button1.Size = New System.Drawing.Size(280, 32)
      Me.button1.TabIndex = 0
      Me.button1.Text = "&Get serial number"
      AddHandler Me.button1.Click, AddressOf Me.Button1Click
      '
      'MainForm
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(292, 45)
      Me.Controls.Add(Me.button1)
      Me.Name = "MainForm"
      Me.Text = "Testform <WMI - Harddisk - Get Serial Number. 2>"
      Me.ResumeLayout(False)
      End Sub
      #End Region

          Private Sub Button1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
              Dim Result As String = GetHarddiskSerialNumber("c:\\")
              If (Not Result Is String.Empty) Then
                  MessageBox.Show(Result, "Info")
              End If
          End Sub
      
          Private Function GetHarddiskSerialNumber(ByVal drv As String) As String
              Try
                  Dim BackSlash As String = "\\"
                  Dim Position As Integer = drv.LastIndexOf(BackSlash)
      
                  If Position > -1 Then
                      drv = drv.Substring(0, drv.Length - 1)
                  Else
                      drv = drv
                  End If
                  Dim mo As New \_
                   ManagementObject("Win32\_LogicalDisk.DeviceID=""" & drv & """")
                  Dim pd As PropertyData = mo.Properties("VolumeSerialNumber")
      
      D Offline
      D Offline
      DaveAuld
      wrote on last edited by
      #2

      offroaderdan wrote:

      however this code is for vb6

      i somehow don't think the code you listed is from vb6......do you mean that the code has been ported from vb6 original code and may contain some errors.

      Dave Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

      O 1 Reply Last reply
      0
      • D DaveAuld

        offroaderdan wrote:

        however this code is for vb6

        i somehow don't think the code you listed is from vb6......do you mean that the code has been ported from vb6 original code and may contain some errors.

        Dave Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

        O Offline
        O Offline
        offroaderdan
        wrote on last edited by
        #3

        umm yeh it contains some errors.

        O 1 Reply Last reply
        0
        • O offroaderdan

          umm yeh it contains some errors.

          O Offline
          O Offline
          offroaderdan
          wrote on last edited by
          #4

          and i ain't very good at solving them if im being perfectly honest. I literally copied and pasted the code into a new form hoever when tryingto run the code this don't work I really need this code so if anybody could help resolve this that would be great

          1 Reply Last reply
          0
          • O offroaderdan

            hi there, i asked a question earlier to try and get information from command prompt howver i seem to have found a better way. here is the code used

            Imports System
            Imports System.Drawing
            Imports System.Windows.Forms
            Imports System.Management

            Namespace VBDotNet_WMI_HD_GetSerialNumber_2

            Public Class MainForm
                Inherits System.Windows.Forms.Form
                Private button1 As System.Windows.Forms.Button
            
                Public Shared Sub Main()
                    Dim fMainForm As New MainForm
                    fMainForm.ShowDialog()
                End Sub
            
                Public Sub New()
                    MyBase.New()
                    Me.InitializeComponent()
                End Sub
            

            #Region " Windows Forms Designer generated code "
            Private Sub InitializeComponent()
            Me.button1 = New System.Windows.Forms.Button
            Me.SuspendLayout()
            '
            'button1
            '
            Me.button1.Location = New System.Drawing.Point(8, 8)
            Me.button1.Name = "button1"
            Me.button1.Size = New System.Drawing.Size(280, 32)
            Me.button1.TabIndex = 0
            Me.button1.Text = "&Get serial number"
            AddHandler Me.button1.Click, AddressOf Me.Button1Click
            '
            'MainForm
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 45)
            Me.Controls.Add(Me.button1)
            Me.Name = "MainForm"
            Me.Text = "Testform <WMI - Harddisk - Get Serial Number. 2>"
            Me.ResumeLayout(False)
            End Sub
            #End Region

                Private Sub Button1Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                    Dim Result As String = GetHarddiskSerialNumber("c:\\")
                    If (Not Result Is String.Empty) Then
                        MessageBox.Show(Result, "Info")
                    End If
                End Sub
            
                Private Function GetHarddiskSerialNumber(ByVal drv As String) As String
                    Try
                        Dim BackSlash As String = "\\"
                        Dim Position As Integer = drv.LastIndexOf(BackSlash)
            
                        If Position > -1 Then
                            drv = drv.Substring(0, drv.Length - 1)
                        Else
                            drv = drv
                        End If
                        Dim mo As New \_
                         ManagementObject("Win32\_LogicalDisk.DeviceID=""" & drv & """")
                        Dim pd As PropertyData = mo.Properties("VolumeSerialNumber")
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Do you have the vb6 code?

            Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

            O 1 Reply Last reply
            0
            • L Lost User

              Do you have the vb6 code?

              Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

              O Offline
              O Offline
              offroaderdan
              wrote on last edited by
              #6

              no i thought it was orignally written in vb6 however it aint i copied and pasted the code into vb.net however when trying to run it this doesn't work?

              D T 2 Replies Last reply
              0
              • O offroaderdan

                no i thought it was orignally written in vb6 however it aint i copied and pasted the code into vb.net however when trying to run it this doesn't work?

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #7

                See my reply to your other post. There's a link to an example of executing a WMI query in there.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008
                But no longer in 2009...

                O 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  See my reply to your other post. There's a link to an example of executing a WMI query in there.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008
                  But no longer in 2009...

                  O Offline
                  O Offline
                  offroaderdan
                  wrote on last edited by
                  #8

                  Thanks for the reply, this has now rely confused me. What would do i simply do something along the lines off Dim instance As ManagementObjectSearcher Dim returnValue As ManagementObjectCollection returnValue = instance.Get() and then something like SELECT SerialNumber FROM Win32_BIOS" ?? i have never really tried anything like this before so its all new to me

                  O 1 Reply Last reply
                  0
                  • O offroaderdan

                    Thanks for the reply, this has now rely confused me. What would do i simply do something along the lines off Dim instance As ManagementObjectSearcher Dim returnValue As ManagementObjectCollection returnValue = instance.Get() and then something like SELECT SerialNumber FROM Win32_BIOS" ?? i have never really tried anything like this before so its all new to me

                    O Offline
                    O Offline
                    offroaderdan
                    wrote on last edited by
                    #9

                    i have tried using the code you said but got this far, how do i fix it?

                    Imports System
                    Imports System.Management
                    Public Class Form1

                    Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                    End Sub
                    Public Overloads Shared Function \_
                        Main(ByVal args() As String) As Integer
                    
                    End Function 'Main
                    
                    Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                        Dim s As New ManagementObjectSearcher( \_
                    
                            select SerialNumber from Win32\_BIOS""     ' the bit of code you told me to put in it!!
                            New EnumerationOptions( \_
                            Nothing, System.TimeSpan.MaxValue, 1, \_
                            True, False, True, True, False, \_
                            True, True))
                        For Each service As ManagementObject In s.Get()
                                'show the instance
                            TextBox1.Text = (service.ToString())
                        Next
                    End Sub
                    

                    End Class

                    thanks Dave for the help!! :)

                    L D J 3 Replies Last reply
                    0
                    • O offroaderdan

                      i have tried using the code you said but got this far, how do i fix it?

                      Imports System
                      Imports System.Management
                      Public Class Form1

                      Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                      End Sub
                      Public Overloads Shared Function \_
                          Main(ByVal args() As String) As Integer
                      
                      End Function 'Main
                      
                      Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                          Dim s As New ManagementObjectSearcher( \_
                      
                              select SerialNumber from Win32\_BIOS""     ' the bit of code you told me to put in it!!
                              New EnumerationOptions( \_
                              Nothing, System.TimeSpan.MaxValue, 1, \_
                              True, False, True, True, False, \_
                              True, True))
                          For Each service As ManagementObject In s.Get()
                                  'show the instance
                              TextBox1.Text = (service.ToString())
                          Next
                      End Sub
                      

                      End Class

                      thanks Dave for the help!! :)

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      something like this

                      Public Sub q()
                          Dim s As New ManagementObjectSearcher( \_
                              "root\\MyApp", \_
                              "SELECT SerialNumber FROM Win32\_BIOS", \_
                              New EnumerationOptions( \_
                              Nothing, System.TimeSpan.MaxValue, 1, \_
                              True, False, True, True, False, \_
                              True, True))
                          For Each service As ManagementObject In s.Get()
                              'show the instance
                              TextBox1.Text = (service.ToString())
                          Next
                      End Sub
                      

                      Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

                      1 Reply Last reply
                      0
                      • O offroaderdan

                        no i thought it was orignally written in vb6 however it aint i copied and pasted the code into vb.net however when trying to run it this doesn't work?

                        T Offline
                        T Offline
                        Thomas Krojer
                        wrote on last edited by
                        #11

                        definitly not ... imports ... not in vb6

                        1 Reply Last reply
                        0
                        • O offroaderdan

                          i have tried using the code you said but got this far, how do i fix it?

                          Imports System
                          Imports System.Management
                          Public Class Form1

                          Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                          End Sub
                          Public Overloads Shared Function \_
                              Main(ByVal args() As String) As Integer
                          
                          End Function 'Main
                          
                          Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                              Dim s As New ManagementObjectSearcher( \_
                          
                                  select SerialNumber from Win32\_BIOS""     ' the bit of code you told me to put in it!!
                                  New EnumerationOptions( \_
                                  Nothing, System.TimeSpan.MaxValue, 1, \_
                                  True, False, True, True, False, \_
                                  True, True))
                              For Each service As ManagementObject In s.Get()
                                      'show the instance
                                  TextBox1.Text = (service.ToString())
                              Next
                          End Sub
                          

                          End Class

                          thanks Dave for the help!! :)

                          D Offline
                          D Offline
                          DaveAuld
                          wrote on last edited by
                          #12

                          Here, Create a new winforms project; Add a reference to System.Management Add 2 List boxes to your form and a button with name to match the code below and Run it.

                          Imports System.Management

                          Public Class Form1

                          Private Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                          
                              'Enumarate Drives List
                              For Each drive As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives
                                  ListBox1.Items.Add(drive.Name)
                              Next
                          
                          End Sub
                          
                          Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                              If ListBox1.SelectedItems.Count > 0 Then
                                  ListBox2.Items.Clear()
                          
                                  'Get the WMI Properties for the Drive and populate the list box
                                  Dim query As New SelectQuery("Win32\_LogicalDisk")
                          
                                  'ManagementObjectSearcher retrieves a collection of WMI objects based on the query.
                                  Dim search As New ManagementObjectSearcher(query)
                          
                                  ' Display each entry for Win32\_LogicalDisk
                                  Dim info As ManagementObject
                                  For Each info In search.Get()
                                      For Each item As PropertyData In info.Properties
                                          ListBox2.Items.Add(item.Name & " : " & item.Value)
                                      Next
                                  Next
                              End If
                          End Sub
                          

                          End Class

                          Dave Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

                          modified on Wednesday, October 28, 2009 1:10 PM

                          O 1 Reply Last reply
                          0
                          • D DaveAuld

                            Here, Create a new winforms project; Add a reference to System.Management Add 2 List boxes to your form and a button with name to match the code below and Run it.

                            Imports System.Management

                            Public Class Form1

                            Private Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                            
                                'Enumarate Drives List
                                For Each drive As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives
                                    ListBox1.Items.Add(drive.Name)
                                Next
                            
                            End Sub
                            
                            Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                If ListBox1.SelectedItems.Count > 0 Then
                                    ListBox2.Items.Clear()
                            
                                    'Get the WMI Properties for the Drive and populate the list box
                                    Dim query As New SelectQuery("Win32\_LogicalDisk")
                            
                                    'ManagementObjectSearcher retrieves a collection of WMI objects based on the query.
                                    Dim search As New ManagementObjectSearcher(query)
                            
                                    ' Display each entry for Win32\_LogicalDisk
                                    Dim info As ManagementObject
                                    For Each info In search.Get()
                                        For Each item As PropertyData In info.Properties
                                            ListBox2.Items.Add(item.Name & " : " & item.Value)
                                        Next
                                    Next
                                End If
                            End Sub
                            

                            End Class

                            Dave Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

                            modified on Wednesday, October 28, 2009 1:10 PM

                            O Offline
                            O Offline
                            offroaderdan
                            wrote on last edited by
                            #13

                            :laugh: :laugh: :laugh: :D works like a treat! However is there any way that i can get drive "C" to appear and in a textbox and have volume serial number of the drive in a txtbox. so it would look something like this text box 1 contents = volume serial "blah blah blah"( number) of "C" I have never really played around with list boxes, i only really know txt boxes and labels!! Many thanks Dave!

                            D 1 Reply Last reply
                            0
                            • O offroaderdan

                              :laugh: :laugh: :laugh: :D works like a treat! However is there any way that i can get drive "C" to appear and in a textbox and have volume serial number of the drive in a txtbox. so it would look something like this text box 1 contents = volume serial "blah blah blah"( number) of "C" I have never really played around with list boxes, i only really know txt boxes and labels!! Many thanks Dave!

                              D Offline
                              D Offline
                              DaveAuld
                              wrote on last edited by
                              #14

                              Thats just basic string manipulation. If you can't work that out you shouldn't be trying to mess about with WMI! Also if you look at the documentation for the classes used, you will see it is also possible to query just the single property you want, rather than have to iterate each one. Go try and figure it out, and then come back with what you tried if you have problems. I am not going to do it all for you!

                              Dave Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

                              O 1 Reply Last reply
                              0
                              • O offroaderdan

                                i have tried using the code you said but got this far, how do i fix it?

                                Imports System
                                Imports System.Management
                                Public Class Form1

                                Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                                End Sub
                                Public Overloads Shared Function \_
                                    Main(ByVal args() As String) As Integer
                                
                                End Function 'Main
                                
                                Private Sub Button1\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                    Dim s As New ManagementObjectSearcher( \_
                                
                                        select SerialNumber from Win32\_BIOS""     ' the bit of code you told me to put in it!!
                                        New EnumerationOptions( \_
                                        Nothing, System.TimeSpan.MaxValue, 1, \_
                                        True, False, True, True, False, \_
                                        True, True))
                                    For Each service As ManagementObject In s.Get()
                                            'show the instance
                                        TextBox1.Text = (service.ToString())
                                    Next
                                End Sub
                                

                                End Class

                                thanks Dave for the help!! :)

                                J Offline
                                J Offline
                                John M Bundy
                                wrote on last edited by
                                #15

                                I'm not sure where you're headed with what you have, but try here, I downloaded the sample and it worked great. It is in Csharp but that is easy to convert if you need to http://www.eggheadcafe.com/articles/20021019.asp[^]

                                1 Reply Last reply
                                0
                                • D DaveAuld

                                  Thats just basic string manipulation. If you can't work that out you shouldn't be trying to mess about with WMI! Also if you look at the documentation for the classes used, you will see it is also possible to query just the single property you want, rather than have to iterate each one. Go try and figure it out, and then come back with what you tried if you have problems. I am not going to do it all for you!

                                  Dave Who am I?: Web|Facebook|Twitter|LinkedIn|Bebo

                                  O Offline
                                  O Offline
                                  offroaderdan
                                  wrote on last edited by
                                  #16

                                  ok i had ago with the changing the code around this is how far i got t cant seem to figure it out all i need is if drive "c" contains a volume serial number then that is placed on a text box i had a go at changing For Each drive As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives ListBox1.Items.Add(drive.Name) Next to Dim drive as system.io.driveInfo listbox1.items.add("C:/") this seemed to work however in list box 2 it still displayed 4 drives?? i tried to extract certain bits of data from lit box to textbox, however this didn't work.

                                  If ListBox2.Items.Contains("VolumeSerialNumber" & " : " & item.value) Then
                                  TextBox1.Text = "VolumeSerialNumber" & " : " & item.value
                                  End If

                                  Is there any way that with each drive a list box can be dedicated to that drive rather than having a massive list? Many thanks

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups