C# 啟動專案直接修改電腦的日期格式
阿新 • • 發佈:2018-11-23
啟動介面新增
1、引用新增
using System.Runtime.InteropServices;
2、方法新增
[DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")] public static extern int GetSystemDefaultLCID(); [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")] public static extern int SetLocaleInfo(intLocale, int LCType, string lpLCData); public const int LOCALE_SLONGDATE = 0x20; public const int LOCALE_SSHORTDATE = 0x1F; public const int LOCALE_STIME = 0x1003; public void SetDateTimeFormat() { try { int x = GetSystemDefaultLCID(); SetLocaleInfo(x, LOCALE_STIME,"HH:mm:ss"); //時間格式 SetLocaleInfo(x, LOCALE_SSHORTDATE, "yyyy-MM-dd"); //短日期格式 SetLocaleInfo(x, LOCALE_SLONGDATE, "yyyy-MM-dd"); //長日期格式 } catch (Exception ex) { Console.WriteLine(ex); } } }
3、在啟動介面的Load事件呼叫方法
SetDateTimeFormat();