locked
Hi Friends, RRS feed

  • Question

  • Hi 2 all,

    How can i get all system time zones using vb.net?

    And also how to set a time zone to my system using vb.net?

    Please give me a solution.

     

    Thanx in advance,

    sreenivas.

    Friday, August 17, 2007 5:46 AM

Answers

  • Hi Srinivasintouch,

     

    Based on your post, you want to get the all system time zones. You need to to read the registry to obtain the all time zones. Here is one code example. You need to put one button control and one listbox  control on the form to run the following code snippet. Hope this helps.

    Code Snippet

    Imports System

    Imports System.Collections.Generic

    Imports System.ComponentModel

    Imports System.Data

    Imports System.Drawing

    Imports System.Text

    Imports System.Windows.Forms

    Imports System.Runtime.InteropServices

    Imports Microsoft.Win32

    Imports System.Collections

     

    Public Class Form1

        Private ZonesHT As New Hashtable() 

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim tz As TimeZoneInformation = DirectCast(ZonesHT(ListBox1.SelectedItem), TimeZoneInformation)

            ClassTime.SetTimeZone(tz)

        End Sub 

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

            ZonesHT = ReadTimeZones.GetTimeZones()

            For Each key As String In ZonesHT.Keys

                ListBox1.Items.Add(key)

            Next

        End Sub

    End Class 

    <StructLayoutAttribute(LayoutKind.Sequential)> _

       Structure SystemTime

        Public year As Short

        Public month As Short

        Public dayOfWeek As Short

        Public day As Short

        Public hour As Short

        Public minute As Short

        Public second As Short

        Public milliseconds As Short

    End Structure 

     

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _

    Public Structure TimeZoneInformation

        Public bias As Integer 

        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _

        Public standardName As String 

        Private standardDate As SystemTime 

        Public standardBias As Integer 

        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=32)> _

        Public daylightName As String 

        Private daylightDate As SystemTime 

        Public daylightBias As Integer

    End Structure 

    Public Class ClassTime 

        <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _

        Private Shared Function GetTimeZoneInformation(ByRef lpTimeZoneInformation As TimeZoneInformation) As Integer

        End Function 

        <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _

        Private Shared Function SetTimeZoneInformation(ByRef lpTimeZoneInformation As TimeZoneInformation) As Boolean

        End Function 

        Public Shared Sub SetTimeZone(ByVal tzi As TimeZoneInformation)

            SetTimeZoneInformation(tzi)

        End Sub 

        Public Shared Function GetTimeZone() As TimeZoneInformation

            Dim tzi As TimeZoneInformation

            Dim currentTimeZone As Integer = GetTimeZoneInformation(tzi)

            Return tzi

        End Function 

    End Class 

    Public Class ReadTimeZones

        Public Shared Function GetTimeZones() As Hashtable 

            Dim hklm As RegistryKey = Registry.LocalMachine

            Dim timeZoneSubKey As String = "Software\Microsoft\Windows NT\CurrentVersion\Time Zones"

            Dim TimeZoneKeys As RegistryKey = hklm.OpenSubKey(timeZoneSubKey) 

            Dim IndividualZone As RegistryKey

            Dim ZonesHT As New Hashtable()

            For Each zonekey As String In TimeZoneKeys.GetSubKeyNames()

                IndividualZone = TimeZoneKeys.OpenSubKey(zonekey)

                Dim TZI As New TimeZoneInformation()

                TZI.standardName = DirectCast(IndividualZone.GetValue("Dlt"), String)

                TZI.daylightName = DirectCast(IndividualZone.GetValue("Display"), String)

                Dim b As Byte() = IndividualZone.GetValue("TZI")

                TZI.bias = BitConverter.ToInt32(b, 0)

                ZonesHT.Add(TZI.daylightName, TZI)

            Next

            Return ZonesHT

        End Function

    End Class

     

     

     

    Thanks for your questions.

     

    Tuesday, August 21, 2007 1:47 PM
    Moderator