|
บทความชิ้นนี้จะเป็นการตรวจสอบเวอร์ชั่นของ Microsoft Office และเวอร์ชั่นของ .NET Framework โดยการเช็คค่าต่างๆผ่านทางรีจีสทรี้ (Registry) ซึ่งแม้ว่าโค้ดที่แอดมินนำมาเสนอนี้ จะเป็นการใช้ Class ก็ตามที ก็เลยอาจจะดูเหมือนว่าล้ำๆไปสักหน่อย แต่ขอให้ทุกๆท่านได้โปรดพิจารณา กระบวนการคิด ว่าแท้ที่จริงแล้วการหาคำตอบออกมา มันเป็น ระดับขั้นพื้นฐาน เท่านั้นเองขอรับกระผม เพราะเป็นแยกแยะ (Parser) ข้อมูลที่เราต้องการออกมาจากชุด String และยังมีการปฏิบัติการที่ทำซ้ำๆ อย่าง MS Office ก็มีทั้ง Word Excel หรือหากเป็น Framework ก็มีหลายเวอร์ชั่น ... อนึ่ง!!! แอดมินไม่สามารถจะอธิบายรายละเอียดได้ทั้งหมด หากท่านอยากทำความเข้าใจให้ดียิ่งขึ้น แอดมินแนะนำให้ไปเปิดคู่มือ หรือ MSDN เพืื่อดูความหมายและรายละเอียดด้วยตัวเองครับ ...
ตัวอย่างของ MS Access ... Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Access.Application\CurVer
มาดูโค้ดกันเถอะ ... ส่วนของ Class
- ' / --------------------------------------------------------------------------------
- ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
- ' / eMail : thongkorn@hotmail.com
- ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
- ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
- ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
- ' / Purpose: Get information about MS Office & .Net Framework Version.
- ' / Microsoft Visual Basic .NET (2010)
- ' /
- ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
- ' / You can modify and/or distribute without to inform the developer.
- ' / --------------------------------------------------------------------------------
- Imports System.IO
- Imports System.Text.RegularExpressions
- Imports Microsoft.Win32
- Public Class clsMicrosoftNet
- #Region "'// - MS Office Version - "
- '// MS Office Applications
- Enum MSOfficeApp
- Access
- Excel
- Outlook
- PowerPoint
- Word
- End Enum
- '// MS Office versions
- Enum Version
- Version95 = 7
- Version97 = 8
- Version2000 = 9
- Version2002 = 10
- Version2003 = 11
- Version2007 = 12
- Version2010 = 14
- Version2013 = 15
- Version2016 = 16
- End Enum
- '// ALL OFFICE VERSIONS
- Public Shared ReadOnly Property AllOfficeVersions() As List(Of String)
- Get
- Dim arrOffice As New List(Of String)
- '// Loop MS Office.
- For Each s As String In [Enum].GetNames(GetType(MSOfficeApp))
- arrOffice.Add(s.Replace("Application", "") + " = " + GetVersionsString(CType([Enum].Parse(GetType(MSOfficeApp), s), MSOfficeApp)))
- Next
- Return arrOffice
- End Get
- End Property
- '// GET VERSIONS STRING
- Public Shared Function GetVersionsString(ByVal app As MSOfficeApp) As String
- Try
- Dim strProgID As String = [Enum].GetName(GetType(MSOfficeApp), app)
- '// RegEdit ... Get version of MS Office, e.g. Access.Application
- '// Get value from ENUM e.g. Access, then take the word ".Application" to the end.
- strProgID = strProgID & "." & "Application"
- Dim regKey As RegistryKey
- '// REGEDIT Read --> "HKLM\Software\Classes" & "Access.Application" & "\CurVer"
- regKey = Registry.LocalMachine.OpenSubKey("Software\Classes" & strProgID & "\CurVer", False)
- If regKey Is Nothing Then Return "No version detected."
- Dim strV As String = regKey.GetValue("", Nothing, RegistryValueOptions.None).ToString
- regKey.Close()
- strV = strV.Replace(strProgID, "").Replace(".", "")
- Return [Enum].GetName(GetType(Version), CInt(strV))
- Catch ex As Exception
- Return String.Empty
- End Try
- End Function
- #End Region '"// - MS Office Version - "
- #Region "'// - Framework - "
- Private Const FRAMEWORKPATH As String = "\Microsoft.NET\Framework"
- Private Const WINDIR As String = "windir"
- Private Const SYSROOT As String = "SystemRoot"
- Public Shared ReadOnly Property ListFrameworkVersions() As List(Of String)
- Get
- Dim arrVersions As New List(Of String)
- Dim strVersion As String = "Unknown"
- '// Loop Framework all versions.
- For Each strFramework As String In Directory.GetDirectories(NetFrameworkInstallationPath, "v*")
- strVersion = ExtractVersion(strFramework)
- If PatternIsVersion(strVersion) Then
- arrVersions.Add(strVersion)
- End If
- Next
- Return arrVersions
- End Get
- End Property
- '// Extract only version At the end of the data
- Private Shared Function ExtractVersion(ByVal pDirectory As String) As String
- '// e.g. --> "C:\Windows\Microsoft.NET\Framework\v1.0.3705"
- Dim intStartIndex As Integer = pDirectory.LastIndexOf("") + 2
- '// Return --> "1.0.3705"
- Return pDirectory.Substring(intStartIndex, pDirectory.Length - intStartIndex)
- End Function
- '// Pattern
- Private Shared Function PatternIsVersion(ByVal pVersion As String) As Boolean
- ' / Using Regular Expressions, Formatting, or Grouping To find the message as we want.
- ' / Remember, Imports System.Text.RegularExpressions
- ' / See more ... http://www.rexegg.com/regex-quickstart.html
- Return New Regex("[0-9](.[0-9]){0,3}").IsMatch(pVersion)
- End Function
- '// .Net Framework Path
- Public Shared ReadOnly Property NetFrameworkInstallationPath() As String
- Get
- Return WindowsPath + FRAMEWORKPATH
- End Get
- End Property
- '// Windows Path
- Public Shared ReadOnly Property WindowsPath() As String
- Get
- Dim strWinDir As String = Environment.GetEnvironmentVariable(WINDIR)
- If String.IsNullOrEmpty(strWinDir) Then
- strWinDir = Environment.GetEnvironmentVariable(SYSROOT)
- End If
- Return strWinDir
- End Get
- End Property
- #End Region '// - Framework -
- End Class
คัดลอกไปที่คลิปบอร์ด
ส่วนของฟอร์มที่เรานำ Class มาใช้งาน ...
- ' / --------------------------------------------------------------------------------
- ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
- ' / eMail : thongkorn@hotmail.com
- ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
- ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
- ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
- ' / Purpose: Get information about MS Office & .Net Framework Version.
- ' / Microsoft Visual Basic .NET (2010)
- ' /
- ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
- ' / You can modify and/or distribute without to inform the developer.
- ' / --------------------------------------------------------------------------------
- Imports Microsoft.Win32
- Public Class frmMicrosoftNet
- Private Sub frmMicrosoftNet_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
- Call MicrosoftNet()
- End Sub
- '// Demonstrate to use Class.
- Private Sub MicrosoftNet()
- ListBox1.Items.Clear()
- '// MS Office Version.
- ListBox1.Items.Add("MS Office.")
- For Each strOffice In clsMicrosoftNet.AllOfficeVersions
- ListBox1.Items.Add(vbTab + strOffice)
- Next
- '// .Net Framework
- ListBox1.Items.Add("-----------------------------------")
- ListBox1.Items.Add(".Net Framework")
- ListBox1.Items.Add("Path: " & clsMicrosoftNet.NetFrameworkInstallationPath)
- For Each strFrameVer In clsMicrosoftNet.ListFrameworkVersions
- ListBox1.Items.Add(vbTab + strFrameVer)
- Next
- ListBox1.Items.Add("-----------------------------------")
- '// Simple Function
- 'MsgBox("MS Office = " & GetOfficeVersion("Access"))
- End Sub
คัดลอกไปที่คลิปบอร์ด
ตัวอย่างการเลือกใช้ฟังค์ชั่นแทน ...
- '// Simple to use Function (OOP = Older Of Programming)
- Private Function GetOfficeVersion(ByVal strProgID As String) As String
- strProgID = strProgID & ".Application"
- Dim regKey As RegistryKey
- '// REGEDIT Read --> "HKLM\Software\Classes" & "Access.Application" & "\CurVer"
- regKey = Registry.LocalMachine.OpenSubKey("Software\Classes" & strProgID & "\CurVer", False)
- If regKey Is Nothing Then Return "No version detected."
- '//
- Dim strV As String = regKey.GetValue("", Nothing, RegistryValueOptions.None).ToString
- regKey.Close()
- '// strV = "Access.Application.14"
- Dim sArr() As String
- '// Split strV with Dot (".")
- sArr = Split(strV, ".")
- '// sArr(0) = "Access", sArr(1) = "Application", sArr(2) = "14"
- '// Return Upper Bound or Highest index.
- Return sArr(UBound(sArr))
- End Function
คัดลอกไปที่คลิปบอร์ด
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่
|
ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง
คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน
x
|