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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值