C#设置系统日期时间格式
?當開發時某些軟件具有特定的日期格式,需要設置系統日期格式,保證軟件正常運行。
DllImport需要using System.Runtime.InteropServices;
?
[DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")]
public static extern int SetLocaleInfo(int Locale, int LCType, string lpLCData);
[DllImport("user32.dll", EntryPoint = "SendMessageTimeout")]
public static extern long SendMessageTimeout(int hWnd, int Msg, int wParam, int lParam, int fuFlags, int uTimeout, ref int lpdwResult);
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
public const int LOCALE_USER_DEFAULT = 0x0400;
public const int LOCALE_SYSTEM_DEFAULT = 0x0800;
public const int LOCALE_SSHORTDATE = 0x1F;
public const int LOCALE_STIMEFORMAT = 0x1003;
public const int HWND_BROADCAST = 0xFFFF;
public const int WM_SETTINGCHANGE = 0x001A;
public const int SMTO_ABORTIFHUNG = 2;
public static void SetDateTimeFormat()
{
int p = 0;
//設置短日期格式
SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, "yyyy-MM-dd");
//設置時間格式,24小時制
SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, "HH:mm:ss");
//設置完成后必須調用,通知其他程序格式已經更改,否則即使是程序自身也不能使用新設置的格式
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0, SMTO_ABORTIFHUNG, 10,ref p);
}
轉載于:https://www.cnblogs.com/diulove/p/8691631.html
總結
以上是生活随笔為你收集整理的C#设置系统日期时间格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud入门之应用程序上下
- 下一篇: [daily] fedora用过光盘做d