ลงชื่อเข้าใช้
กระดานข่าว VB > เว็บบอร์ดเปิดโค้ดต้นฉบับ
ส่ง|
ดู6350|โพสต์2|บุ๊คมาร์ก

บทความนี้อธิบายถึงวิธีการเก็บข้อมูลแบบกำหนดเองในแฟ้มการตั้งค่าคอนฟิก (.config ของโปรแกรม) ที่คุณสามารถเรียกดูได้ในภายหลัง ในระหว่างการรันไทม์ของแอพพลิเคชันที่เกี่ยวข้อง มีประโยชน์เมื่อคุณต้องกำหนดข้อมูลที่เกี่ยวข้องกับแอพพลิเคชัน (แปลจาก Microsoft 5555+ ... https://support.microsoft.com/th-th/help/815786/how-to-store-and-retrieve-custom-information-from-an-application-confi)

โค้ด:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <configuration>
  3.   <appSettings>
  4.     <add key="Server" value="(local)" />
  5.     <add key="Database" value="G2GNETDB" />
  6.     <add key="uid" value="admin" />
  7.     <add key="pwd" value="admin" />
  8.     <add key="providerType" value="Sql" />
  9.     <add key="databaseVersion" value="MSSQL2016" />
  10.   </appSettings>
  11. </configuration>
รูปร่างหน้าตาของไฟล์ app.config ก็จะอยู่ในรูปแบบของ XML (eXtensible Markup Language) อีกแล้วครับท่าน ...

อันดับแรกต้องอ้างอิง Library หรือ การเรียกใช้ Reference --> System.Configuration

โค้ด:

  1.         '<?xml version="1.0" encoding="utf-8"?>
  2.         '<configuration>
  3.         '<appSettings>
  4.         '<add key="Server" value="(local)" />
  5.         '<add key="Database" value="G2GNETDB" />
  6.         '<add key="uid" value="admin" />
  7.         '<add key="pwd" value="admin" />
  8.         '<add key="providerType" value="Sql" />
  9.         '<add key="databaseVersion" value="MSSQL2016" />
  10.         '</appSettings>
  11.         '</configuration>
  12.         Try
  13.             Dim reader As New System.Configuration.AppSettingsReader
  14.             txtServer.Text = reader.GetValue("server", GetType(String))
  15.             txtDatabase.Text = reader.GetValue("database", GetType(String))
  16.             txtUID.Text = reader.GetValue("uid", GetType(String))
  17.             txtPwd.Text = reader.GetValue("pwd", GetType(String))
  18.             btnSave.Enabled = True

  19.         Catch ex As Exception
  20.             MessageBox.Show(ex.Message, "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  21.         End Try
มีอยู่หลายวิธีในการเรียกข้อมูลออกมาจาก app.config สำหรับตัวอย่างนี้แอดมินใช้ Class AppSettingsReader เพื่อเลือกการอ่านเฉพาะค่าออกมาเลย ... (https://msdn.microsoft.com/en-us/library/system.configuration.appsettingsreader(v=vs.110).aspx)

โค้ด:

  1.         Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
  2.         Dim settings As KeyValueConfigurationCollection = config.AppSettings.Settings
  3.         Try
  4.             '// update SaveBeforeExit
  5.             settings("server").Value = txtServer.Text
  6.             settings("database").Value = txtDatabase.Text
  7.             settings("uid").Value = txtUID.Text
  8.             settings("pwd").Value = txtPwd.Text
  9.             '//save the file
  10.             config.Save(ConfigurationSaveMode.Modified)
  11.             ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name)

  12.             MessageBox.Show("Update Setting.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  13.             btnSave.Enabled = False

  14.         Catch ex As Exception
  15.             MessageBox.Show(ex.Message)
  16.         End Try
สำหรับการ Save ข้อมูลกลับเข้าไปใน app.config

IMPORTANT - สำคัญเชียวน่ะจะบอกให้

เมื่อเราสั่งทดสอบโปรแกรมด้วยการกด F5 หากเกิดการเปลี่ยนแปลงใดๆของข้อมูล เราจะมองไม่เห็น เพราะว่าตำแหน่งของไฟล์ Execute กับไฟล์ app.config อยู่คนละตำแหน่งกัน ... การใช้วิธีนี้ต้อง Build Project ก่อน จากนั้น App.Config มันจะถูกคัดลอกนำไปไว้ยังโฟลเดอร์ Debug/Release (ขึ้นกับว่าเราไปกำหนดให้ Execute ที่ไหน) จากนั้นมันก็สร้าง ชื่อโปรเจค.exe.config เช่นตัวอย่างนี้คือ MyAppConfig ... เราจะรันเพื่อให้สามารถเปลี่ยนแปลงข้อมูลได้ที่นี่
มาดูโค้ดฉบับเต็ม ...

โค้ด:

  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. ' /
  8. ' / Purpose: Using app.config for user defined @runtime parameters.
  9. ' / Microsoft Visual Basic .NET (2010)
  10. ' /
  11. ' / This is open source code under @CopyLeft by Thongkorn Tubtimkrob.
  12. ' / You can modify and/or distribute without to inform the developer.
  13. ' / --------------------------------------------------------------------------------
  14. Imports System.Configuration

  15. Public Class frmConfig
  16.     Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
  17.         '<?xml version="1.0" encoding="utf-8"?>
  18.         '<configuration>
  19.         '<appSettings>
  20.         '<add key="Server" value="(local)" />
  21.         '<add key="Database" value="G2GNETDB" />
  22.         '<add key="uid" value="admin" />
  23.         '<add key="pwd" value="admin" />
  24.         '<add key="providerType" value="Sql" />
  25.         '<add key="databaseVersion" value="MSSQL2016" />
  26.         '</appSettings>
  27.         '</configuration>
  28.         Try
  29.             Dim reader As New System.Configuration.AppSettingsReader
  30.             txtServer.Text = reader.GetValue("server", GetType(String))
  31.             txtDatabase.Text = reader.GetValue("database", GetType(String))
  32.             txtUID.Text = reader.GetValue("uid", GetType(String))
  33.             txtPwd.Text = reader.GetValue("pwd", GetType(String))
  34.             btnSave.Enabled = True

  35.         Catch ex As Exception
  36.             MessageBox.Show(ex.Message, "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  37.         End Try

  38.     End Sub

  39.     Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
  40.         Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
  41.         Dim settings As KeyValueConfigurationCollection = config.AppSettings.Settings
  42.         Try
  43.             '// update SaveBeforeExit
  44.             settings("server").Value = txtServer.Text
  45.             settings("database").Value = txtDatabase.Text
  46.             settings("uid").Value = txtUID.Text
  47.             settings("pwd").Value = txtPwd.Text
  48.             '//save the file
  49.             config.Save(ConfigurationSaveMode.Modified)
  50.             ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name)
  51.             '// Or use sub program from MSDN.
  52.             '//AddUpdateAppSettings("database", txtDatabase.Text)

  53.             MessageBox.Show("Update Setting.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  54.             btnSave.Enabled = False

  55.         Catch ex As Exception
  56.             MessageBox.Show(ex.Message)
  57.         End Try
  58.     End Sub

  59.     '// If you want to learn more.
  60.     '// https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx
  61.     Sub AddUpdateAppSettings(key As String, value As String)
  62.         Try
  63.             Dim configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
  64.             Dim settings = configFile.AppSettings.Settings
  65.             If IsNothing(settings(key)) Then
  66.                 settings.Add(key, value)
  67.             Else
  68.                 settings(key).Value = value
  69.             End If
  70.             configFile.Save(ConfigurationSaveMode.Modified)
  71.             ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name)
  72.         Catch e As ConfigurationErrorsException
  73.             MessageBox.Show("Error writing app settings: " & vbCrLf & e.Message)
  74.         End Try
  75.     End Sub

  76.     Private Sub frmConfig_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  77.         btnSave.Enabled = False
  78.     End Sub

  79.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  80.         Me.Close()
  81.     End Sub

  82.     Private Sub frmConfig_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  83.         Me.Dispose()
  84.         Application.Exit()
  85.     End Sub

  86. End Class
ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010)
ไฟล์แนบ: คุณจำเป็นต้องลงชื่อเข้าใช้ก่อนจึงจะสามารถดูและดาวน์โหลดไฟล์แนบได้ หากยังไม่มีบัญชีหรือยังไม่ได้เป็นสมาชิก กรุณาลงทะเบียน
ขอบคุณครับ
MMEE007 ดูทั้งหมด
2022-12-11 21:12:32
ขอบพระคุณคัพ อาจารย์

G2GNet.com

Powered by Discuz! X3.4

โฮมเพจ|รูปแบบทั่วไป|รูปแบบโมเดิร์น|รูปแบบคอมพิวเตอร์