How can I change a Windows user's regional settings/date format?

前端 未结 4 1459
独厮守ぢ
独厮守ぢ 2020-12-21 00:19

I use a VB6/COM+ application which outputs date/time values based on the short date settings in the Control Panel, Regional Settings, for the user that runs it. The program

相关标签:
4条回答
  • 2020-12-21 00:43

    code:

    Imports System.Threading
    Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US", False)
    Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\Control Panel\International", "sShortDate", "M/d/yyyy")
    
    0 讨论(0)
  • 2020-12-21 00:43

    While persistently changing a user's culture (regional settings) is to be done cautiously, there are legitimate use cases.

    On Windows 8 and Windows Server 2012 and above, Windows PowerShell comes with the
    Set-Culture cmdlet, which is the programmatic equivalent of choosing a different region via Control Panel (intl.cpl).

    For instance, Set-Culture fr-CA is the equivalent of interactively choosing region French (Canada) for the current user.

    Caveat: Mixed cultures such as en-DE (sic) appear not to work as of Windows PowerShell v5.1 - see this answer of mine.

    While it won't be fast, it is possible to call PowerShell commands from C#.

    0 讨论(0)
  • 2020-12-21 00:56

    This is specifically discouraged by Microsoft. Any solution you may come up with will be a filthy hack that will probably stop working soon.

    Think of it this way: who are you to decide those settings? Don't you think that's the user's decision?

    Back on topic: find an unambiguous format for the applications to communicate in, such as YYYYMMDD. The application that displays can then simply respect the actual user settings, as it should.

    But, since you can't change it, just poke into the registry:

    Current user:

    HKEY_CURRENT_USER\Control Panel\International
    

    Specific user:

    HKEY_USERS\(user SID)\Control Panel\International
    

    Default user:

    HKEY_USERS\.DEFAULT\Control Panel\International
    

    sShortDate is probably the value you want to change.

    0 讨论(0)
  • 2020-12-21 01:03

    If you are going to modify the profile to suit your needs, why not just ignore the profile settings and hardcode the format you want in your app?

    0 讨论(0)
提交回复
热议问题