ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3888|ตอบกลับ: 1

[VB.NET] การตรวจสอบเวอร์ชั่นของ MS Office และ .Net Framework

[คัดลอกลิงก์]

309

กระทู้

500

โพสต์

6032

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6032



บทความชิ้นนี้จะเป็นการตรวจสอบเวอร์ชั่นของ 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
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Purpose: Get information about MS Office & .Net Framework Version.
  8. ' / Microsoft Visual Basic .NET (2010)
  9. ' /
  10. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  11. ' / You can modify and/or distribute without to inform the developer.
  12. ' / --------------------------------------------------------------------------------
  13. Imports System.IO
  14. Imports System.Text.RegularExpressions
  15. Imports Microsoft.Win32

  16. Public Class clsMicrosoftNet

  17. #Region "'// - MS Office Version - "
  18.     '// MS Office Applications
  19.     Enum MSOfficeApp
  20.         Access
  21.         Excel
  22.         Outlook
  23.         PowerPoint
  24.         Word
  25.     End Enum

  26.     '// MS Office versions   
  27.     Enum Version
  28.         Version95 = 7
  29.         Version97 = 8
  30.         Version2000 = 9
  31.         Version2002 = 10
  32.         Version2003 = 11
  33.         Version2007 = 12
  34.         Version2010 = 14
  35.         Version2013 = 15
  36.         Version2016 = 16
  37.     End Enum

  38.     '// ALL OFFICE VERSIONS
  39.     Public Shared ReadOnly Property AllOfficeVersions() As List(Of String)
  40.         Get
  41.             Dim arrOffice As New List(Of String)
  42.             '// Loop MS Office.
  43.             For Each s As String In [Enum].GetNames(GetType(MSOfficeApp))
  44.                 arrOffice.Add(s.Replace("Application", "") + " = " + GetVersionsString(CType([Enum].Parse(GetType(MSOfficeApp), s), MSOfficeApp)))
  45.             Next
  46.             Return arrOffice
  47.         End Get
  48.     End Property

  49.     '// GET VERSIONS STRING
  50.     Public Shared Function GetVersionsString(ByVal app As MSOfficeApp) As String
  51.         Try
  52.             Dim strProgID As String = [Enum].GetName(GetType(MSOfficeApp), app)
  53.             '// RegEdit ... Get version of MS Office, e.g. Access.Application
  54.             '// Get value from ENUM e.g. Access, then take the word ".Application" to the end.
  55.             strProgID = strProgID & "." & "Application"
  56.             Dim regKey As RegistryKey
  57.             '// REGEDIT Read --> "HKLM\Software\Classes" & "Access.Application" & "\CurVer"
  58.             regKey = Registry.LocalMachine.OpenSubKey("Software\Classes" & strProgID & "\CurVer", False)
  59.             If regKey Is Nothing Then Return "No version detected."
  60.             Dim strV As String = regKey.GetValue("", Nothing, RegistryValueOptions.None).ToString
  61.             regKey.Close()
  62.             strV = strV.Replace(strProgID, "").Replace(".", "")
  63.             Return [Enum].GetName(GetType(Version), CInt(strV))
  64.         Catch ex As Exception
  65.             Return String.Empty
  66.         End Try
  67.     End Function

  68. #End Region '"// - MS Office Version - "

  69. #Region "'// - Framework - "
  70.     Private Const FRAMEWORKPATH As String = "\Microsoft.NET\Framework"
  71.     Private Const WINDIR As String = "windir"
  72.     Private Const SYSROOT As String = "SystemRoot"

  73.     Public Shared ReadOnly Property ListFrameworkVersions() As List(Of String)
  74.         Get
  75.             Dim arrVersions As New List(Of String)
  76.             Dim strVersion As String = "Unknown"
  77.             '// Loop Framework all versions.
  78.             For Each strFramework As String In Directory.GetDirectories(NetFrameworkInstallationPath, "v*")
  79.                 strVersion = ExtractVersion(strFramework)
  80.                 If PatternIsVersion(strVersion) Then
  81.                     arrVersions.Add(strVersion)
  82.                 End If
  83.             Next

  84.             Return arrVersions
  85.         End Get
  86.     End Property

  87.     '// Extract only version At the end of the data
  88.     Private Shared Function ExtractVersion(ByVal pDirectory As String) As String
  89.         '// e.g. --> "C:\Windows\Microsoft.NET\Framework\v1.0.3705"
  90.         Dim intStartIndex As Integer = pDirectory.LastIndexOf("") + 2
  91.         '// Return --> "1.0.3705"
  92.         Return pDirectory.Substring(intStartIndex, pDirectory.Length - intStartIndex)
  93.     End Function

  94.     '// Pattern
  95.     Private Shared Function PatternIsVersion(ByVal pVersion As String) As Boolean
  96.         ' / Using Regular Expressions, Formatting, or Grouping To find the message as we want.
  97.         ' / Remember, Imports System.Text.RegularExpressions
  98.         ' / See more ... http://www.rexegg.com/regex-quickstart.html
  99.         Return New Regex("[0-9](.[0-9]){0,3}").IsMatch(pVersion)
  100.     End Function

  101.     '// .Net Framework Path
  102.     Public Shared ReadOnly Property NetFrameworkInstallationPath() As String
  103.         Get
  104.             Return WindowsPath + FRAMEWORKPATH
  105.         End Get
  106.     End Property

  107.     '// Windows Path
  108.     Public Shared ReadOnly Property WindowsPath() As String
  109.         Get
  110.             Dim strWinDir As String = Environment.GetEnvironmentVariable(WINDIR)
  111.             If String.IsNullOrEmpty(strWinDir) Then
  112.                 strWinDir = Environment.GetEnvironmentVariable(SYSROOT)
  113.             End If

  114.             Return strWinDir
  115.         End Get
  116.     End Property

  117. #End Region '// - Framework -

  118. End Class
คัดลอกไปที่คลิปบอร์ด

ส่วนของฟอร์มที่เรานำ Class มาใช้งาน ...
  1. ' / --------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' / Purpose: Get information about MS Office & .Net Framework Version.
  8. ' / Microsoft Visual Basic .NET (2010)
  9. ' /
  10. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  11. ' / You can modify and/or distribute without to inform the developer.
  12. ' / --------------------------------------------------------------------------------

  13. Imports Microsoft.Win32

  14. Public Class frmMicrosoftNet

  15.     Private Sub frmMicrosoftNet_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  16.         Call MicrosoftNet()
  17.     End Sub

  18.     '// Demonstrate to use Class.
  19.     Private Sub MicrosoftNet()
  20.         ListBox1.Items.Clear()
  21.         '// MS Office Version.
  22.         ListBox1.Items.Add("MS Office.")
  23.         For Each strOffice In clsMicrosoftNet.AllOfficeVersions
  24.             ListBox1.Items.Add(vbTab + strOffice)
  25.         Next
  26.         '// .Net Framework
  27.         ListBox1.Items.Add("-----------------------------------")
  28.         ListBox1.Items.Add(".Net Framework")
  29.         ListBox1.Items.Add("Path: " & clsMicrosoftNet.NetFrameworkInstallationPath)
  30.         For Each strFrameVer In clsMicrosoftNet.ListFrameworkVersions
  31.             ListBox1.Items.Add(vbTab + strFrameVer)
  32.         Next
  33.         ListBox1.Items.Add("-----------------------------------")

  34.         '// Simple Function
  35.         'MsgBox("MS Office = " & GetOfficeVersion("Access"))
  36.     End Sub
คัดลอกไปที่คลิปบอร์ด

ตัวอย่างการเลือกใช้ฟังค์ชั่นแทน ...
  1.     '// Simple to use Function (OOP = Older Of Programming)
  2.     Private Function GetOfficeVersion(ByVal strProgID As String) As String
  3.         strProgID = strProgID & ".Application"
  4.         Dim regKey As RegistryKey
  5.         '// REGEDIT Read --> "HKLM\Software\Classes" & "Access.Application" & "\CurVer"
  6.         regKey = Registry.LocalMachine.OpenSubKey("Software\Classes" & strProgID & "\CurVer", False)
  7.         If regKey Is Nothing Then Return "No version detected."
  8.         '//
  9.         Dim strV As String = regKey.GetValue("", Nothing, RegistryValueOptions.None).ToString
  10.         regKey.Close()

  11.         '// strV = "Access.Application.14"
  12.         Dim sArr() As String
  13.         '// Split strV with Dot (".")
  14.         sArr = Split(strV, ".")
  15.         '// sArr(0) = "Access", sArr(1) = "Application", sArr(2) = "14"
  16.         '// Return Upper Bound or Highest index.
  17.         Return sArr(UBound(sArr))
  18.     End Function
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

58

โพสต์

10

เครดิต

Member

Rank: 2

เครดิต
10
โพสต์ 2022-10-25 19:56:22 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-20 13:38 , Processed in 0.200676 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้