หมวดหมู่: C#

C#: แสดง program versionC#: แสดง program version

โปรแกรมถ้าหากมีแก้ไขกันหลาย ๆ ครั้ง ก็ควรจะแสดงให้เห็นได้ง่าย ๆ ว่า user ใช้ version ไหนอยู่ จะได้ง่ายในการดูแล

การใส่ version ใน .Net จะเรียกว่า set assembly ทำได้ง่าย ๆ โดย

  1. ไปที่ Solution Explorer (ขวามือ)
  2. คลิกขวาตรง Project File (ใต้คำว่า Solution) เลือก Properties
  3. กรอกข้อมูล Assembly Name
  4. คลิก Assembly Information…
  5. ใส่เลข Assembly version: และข้อมูลอื่น ๆ

จากนั้นแก้ form ที่ต้องการให้แสดงเวอร์ชั่น โดยแทรก

AssemblyName thisAssemName = this.GetType () .Assembly.GetName () ;
this.Text = thisAssemName.Name + " Version " + thisAssemName.Version;

เช่น FormMain.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrototypeCSharp
{
 public partial class FormMain : Form
 {
 public FormMain () {
 InitializeComponent () ;

 AssemblyName thisAssemName = this.GetType () .Assembly.GetName () ;

 this.Text = thisAssemName.Name + " Version " + thisAssemName.Version;

 }
 }
}

เพิ่มเติม ถ้าต้องการแสดง environment ที่โปรแกรมกำลังใช้อยู่อาจจะเพิ่ม configurations ตามวิธี C#: Configurations ก็อาจจะแก้เป็น

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PrototypeCSharp
{
 public partial class FormMain : Form
 {
 public FormMain () {
 InitializeComponent () ;

 AssemblyName thisAssemName = this.GetType () .Assembly.GetName () ;

 if (Configurations.environment == "PRODUCTION") {
 this.Text = thisAssemName.Name + " Version " + thisAssemName.Version;
 }
 else
 {
 this.Text = thisAssemName.Name + " Version " + thisAssemName.Version + " " + Configurations.environment;
 }
 }
 }
}

อ่านเพิ่มเติม