Skip to content
No results
  • About Me (Resume)
  • Meetup Note/Share
  • เงินทอง งอกเงย
naiwaen@DebuggingSoft วงแหวนเว็บ
  • About Me (Resume)
  • Meetup Note/Share
  • เงินทอง งอกเงย
naiwaen@DebuggingSoft วงแหวนเว็บ

[C#] เก็บข้อมูลใช้งานครั้งล่าสุด หรือ เก็บค่า Config ผ่าน Project Setting

  • AdminpingAdminping
  • July 6, 2014
  • C Sharp

บางครั้งเวลาเราพัฒนา Application ขึ้นมา เราอยากเก็บค่า Config บางอย่าง เช่น username ครั้งสุดท้ายที่ใช้ Log , ชื่อ Database ที่ใช้งานประจำ หรือเป็นขนาดความกว้าง ยาวของหน้าจอครั้งล่าสุด โดยอาจเราจะเก็บไว้ Text File, Registry หรือเก็บใน Project Setting ที่ทาง .Net Framework ได้เตรียมไว้ให้ โดยวันนี้ผมขอแนะนำการเก็บค่า Config ต่างๆลงใน Project Setting กันครับ

คลิกขวาที่ Project File เลือก properties >> เลือกที่ส่วน Setting จากนั้นกำหนดข้อมูล

  • Name : ชื่อ Setting
  • Type : Data type ของ Setting โดย User สามารถกำหนดได้ว่าจะเก็บข้อมูลเป็น String, int หรือ boolean เป็นต้น
  • Scope : ขอบเขตของ Setting นี้ว่าเป็นแบบ Application(ทีผลกับทุก user บนเครื่อง) หรือ เป็นแบบ User(แยก Config ตาม user ที่ใช้งาน)
  • Roaming: กำหนดรูปแบบ Config
    • ถ้า set เป็น True คือ สามารถเก็บ Config ไปใช้งานบนคอมพิวเตอร์เครื่องใดๆก็ได้บนเครือข่าย ที่มีการ Set Active Directory และกำหนด Roaming User Profiles ไว้ ยกตัวอย่างเช่น ถ้า Application ของเราทำงานบนเครื่อง Computer A โดย User Adminping ซึ่งอยู่ใน Active Directory เมื่อ Adminping ไปใช้งานเครื่อง Computer ฺฺฺB ค่า Config ที่เราเคยตั้งไว้จะถูกดึงขึ้นมาพร้อมใช้งานทันที
    • ถ้า set เป็น False คือ ใช้งาน Standalone เก็บค่า Config เฉพาะเครื่องที่ลง Application ของเราเท่านั้น

1

การกำหนด Config สามารถทำได้ โดยสามารถเรียกใช้ผ่านคำสั่ง ดังนี้

Properties.Settings.Default.ชื่อ Config

ตัวอย่าง เช่น

[c language="#"]
Properties.Settings.Default.LastConfigName = "AYF-SITE Config";
[/c]

หากต้องการบันทึกค่า Config นั้นเราสามารถใช้ Method Save ตามตัวอย่าง ดัง Code ต่อไปนี้

[c language="#"]
Properties.Settings.Default.LastConfigID = 23;
Properties.Settings.Default.LastConfigName = "AYF-SITE Config";
Properties.Settings.Default.Save();
[/c]

หากต้องการเรียกใช้ Config นั้น เราสามารถทำได้ โดยใช้ Code ต่อไปนี้

[c language="#"]
txtConfigID.Text = Properties.Settings.Default.LastConfigID.toString();
txtConfigName.Text =Properties.Settings.Default.LastConfigName.toString();
[/c]

หากต้องการ Clear ค่า Config เราสามารถใช้ Method Reset(); ได้

ถ้าอยากรู้ว่า Config ที่เรานั้นสร้างขึ้นมา ถูกจัดเก็บไว้ที่ไหน ให้ลองไปดูใน AppConfig พบ Section เพิ่มขึ้นมา ดังตัวอย่างนี้

[xml]
<userSettings>
<InvConfig.Properties.Settings>
<setting name="LastConfigID" serializeAs="String">
<value>0</value>
</setting>
<setting name="LastConfigName" serializeAs="String">
<value />
</setting>
</InvConfig.Properties.Settings>
</userSettings>
[/xml]

และถ้าอยากรู้อีกว่าค่าที่เราแก้ไป เก็บอยู่ที่ไหนในคอมพิวเตอร์ของเราในกรณี

  • non-roaming  user settings ให้ไปดูที
%USERPROFILE%\Local Settings\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config
  • roaming user settings ให้ไปดูที่
%USERPROFILE%\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config

หมายเหตุ: วิธีการนี้ใช้งานได้กับระบบปฏิบัติการตั้งแต่ Windows Vista เป็นต้นไปนะ

Share this:

  • Tweet
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp

Like this:

Like Loading...

Related


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts sent to your email.

Tags
# Active Directory# store setting# config# Roaming user profile

About me

Adminping

Adminping

My name is Chatri Ngambenchawong and I am a Software Developer, Blogger, Geek and FoodMania ^__^

View Full Profile →

Donate (ร่วมสนับสนุน)

Facebook Page

Facebook Page

Categories

Archives

Pages

  • About Me (Resume)
  • Meetup Note/Share
  • นโยบายการใช้คุกกี้ (Cookies Policy)
  • นโยบายความเป็นส่วนตัวของข้อมูล (Privacy Policy)
  • เงินทอง งอกเงย

Recent Posts

  • [CR] Starbucks Chicken Club Sandwich
  • เวลามี Merge Request / Pull Request มีแนวทางการตรวจยังไง
  • Spring Test Error java.lang.ClassNotFoundException: org.junit.platform.engine.reporting.OutputDirectoryProvider
  • [CR] Starbucks Spring Onion & Cream Cheese Bagel
  • อยู่ๆ Test Report จาก dotnet test ก็หาย

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Related Posts

C# Logo

[C#] แก้ปัญหา Could not load file or assembly ‘System.ComponentModel.Annotations, Version=X.Y.0.0

  • August 19, 2022
C# Logo

[C#] DTO/Mapper Class Example

  • October 13, 2018
Ref https://pixabay.com/th/การวิเคราะห์-ข้อมูล-นวัตกรรม-3088958/

[C#] ให้ Log4net เขียน Log แยกตาม Process Id ของ Application

  • April 16, 2018

ถ้าสนใจ Blog สรุปต่างๆ ลองมา Subscribe กันได้ครับ เดี๋ยวจะมีเมล์จาก donotreply@wordpress.com มาให้กด Confirm อีกทีครับ

  • About Me (Resume)
  • นโยบายการใช้คุกกี้ (Cookies Policy)
Copyright © 2025 - WordPress Theme by CreativeThemes
Manage Cookie Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}
%d