顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2021年4月11日 星期日

[C#] 以程式更新app.config之AppSettings保留註解(By XDocument)

[C#] 以程式更新app.config之AppSettings保留註解(By XDocument)

更新app.config之appSettings(不保留註解)

  • 原app.config內容
    原app.config內容

  • 執行程式

    class Program
    {
        static void Main(string[] args)
        {
            // 寫入config序號
            UpdateSetting("TestSerial", "02");
        }
    
    
        /// <summary>
        /// 更新App.Config 中AppSetting 
        /// </summary>
        /// <param name="key">AppSetting Name</param>
        /// <param name="updateValue">AppSetting Value</param>
        private static void UpdateSetting(string key, string updateValue)
        {
            Configuration configuration =     ConfigurationManager.OpenExeConfiguration(    Assembly.GetExecutingAssembly().Location);
            configuration.AppSettings.Settings[key].Value = updateValue;
            configuration.Save();
        }
    }
    
  • 更新後app.config內容(註解被清除)
    更新後app.config內容(註解被清除)

更新app.config之appSettings(保留註解)

  • 原app.config內容
    原app.config內容

  • 執行程式

    class Program
    {
        static void Main(string[] args)
        {
            // 寫入config序號
            SaveSettingByXDocument("TestSerial", "02");
        }
    
        /// <summary>
        /// 新增/更新App.Config 中AppSetting 
        /// </summary>
        /// <param name="key">AppSetting Name</param>
        /// <param name="updateValue">AppSetting Value</param>
        private static void SaveSettingByXDocument(string key, string value)
        {
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
            var configPath = configuration.AppSettings.ElementInformation.Source;
    
            // 讀取config檔案
            XDocument document = XDocument.Load(configPath);
            if (document?.Root == null)
            {
                return;
            }
    
            // 取得appSettings節點
            XContainer appSettings = document.Element("configuration").Element("appSettings");
    
            // 取得指定key的節點
            XElement appSetting = appSettings.Elements("add").FirstOrDefault(x => x.Attribute("key").Value == key);
            if (appSetting != null)
            {
                appSetting.Attribute("value").Value = value;
                document.Save(configPath);
            }
            else
            {
                // 新增config設定
                appSettings.Add(new XElement("add",
                                    new XAttribute("key", key),
                                    new XAttribute("value", value)));
    
                document.Save(configPath);
            }
    
        }
    }
    
  • 更新後app.config內容(註解保留)
    更新後app.config內容(註解保留)



如有錯誤或建議,歡迎留言指教,謝謝!!
(相關內容如有侵犯隱私或著作權,請協助通知刪除,感謝)

2020年12月6日 星期日

[C#] web.config 設定含有&字元

[C#] web.config 設定含有&字元

[C#] Web.Config 設定含有&字元

簡介

當在Web.config內的appSettings設定含有&的字元時,如未處理則可能會造成應用程式無法啟動,這裡記錄一下如何處理此問題。

2020年8月16日 星期日

[C#] 檔案寫入UTF8 或 UTF8 BOM

[C#] 檔案寫入UTF8 或 UTF8 BOM

[C#] 檔案寫入UTF8 或 UTF8 BOM

簡介

一開始使用靜態函式Encoding.UTF8寫入txt檔時,產生檔案為UTF8 with BOM,為了輸出UTF8 without BOM做一下筆記。

2020年8月15日 星期六

[C#] 西元年轉換為民國年

[C#] 西元年轉換為民國年

[C#] 西元年轉換為民國年

簡介

由於在維護舊程式,發現還有使用西元年減掉1911來取得民國年,這裡稍微紀錄一下修改方式。

2019年5月19日 星期日

[.NET Core] 使用Big5編碼

[.NET Core] 使用big5編碼

問題

當使用 .Net Core 執行 Encoding.GetEncoding("big5")時會發生Exception

System.ArgumentException: ''big5' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method.'

Encoding.GetEncoding("big5")錯誤

2019年2月25日 星期一

[C#] Math.Round 數字四捨五入

[C#] Math.Round 數字四捨五入

簡介

在Code Review的時候,會看到同仁用Math.RoundConvert.ToInt32做四捨五入,但常常沒注意到程式裡的小細節,如果使用預設的話其實是四捨六入五成雙,我們來看看下面的例子比較清楚。

2019年1月22日 星期二

[C#] 智付通SPGateway(藍新)金流串接

[C#] 智付通SPGateway(藍新)金流串接.md

簡介

最近在用C#串接智付通spgateway(藍新)金流,文件主要以php sample code為主,C#部分的內容說明的比較不是這麼詳細,所以串接上遇到了一些問題,藉此來稍微紀錄一下,讓有遇到相同問題的人也能順利通過。(文章最後有提供C#測試的Sample Code)

2019年1月11日 星期五

[C#] 各時區間時間轉換(DateTimeOffset 修改時區)

[C#] 各時區間時間轉換(DateTimeOffset 修改時區)

最近介接金流需要回傳目前時間或截止時間,但擔心Server設定的時區可能不是台灣時區,所以為了以防萬一就先把時區轉換為台灣時區,再將時間傳送到金流端。

2019年1月10日 星期四

[C#] 時間轉換為Unix TimeStamp

[C#] 時間轉換為Unix TimeStamp

簡介

Unix TimeStamp為格林威治時間1970年1月1日00:00:00到當前時間的秒數。