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

4

5

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 that then parses that output has a configurable setting for the date format it expects, and presents in the UI.

e.g. If the regional setting for the user is set to mm/dd/yyyy, and it outputs 06/18/2009, the application expecting "18/06/2009" fails with "String was not recognized as a valid DateTime".

As we usually run this application as a service account, which we have not logged in as interactively to create a profile, we generally set the correct date format and then tick the "Apply all settings to the current user account and the default user profile" option.

I would like to be make the C# configuration utility I have written for this mess to be able to set the date format programmatically for a given user.

Edit I would like nothing more than to change the code, but do not have the ability to do so at this time.

I also know that what I am asking is a bad thing to do. With regards to "it should be the user's choice" - I am that user, as I create it explicitly for the task; I just want to set the date format by a scripted method, rather than having to do the clicking myself.

Beaudoin answered 18/6, 2009 at 17:39 Comment(0)
R
17

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.

Riendeau answered 18/6, 2009 at 18:3 Comment(2)
I know that it's a bad thing to do. I am that user, I create it explicitly for the task; I just want to set the date format by a scripted method, rather than having to do the clicking myself. I would like nothing more than to change the code, but do not have the ability to do so.Beaudoin
Thanks - I'm pretty confident your answer will work, but either way your pragmatic attitude to the problem definitely deserves the rep.Beaudoin
G
2

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?

Gemperle answered 18/6, 2009 at 17:56 Comment(0)
N
1

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")
Nashbar answered 13/12, 2016 at 18:50 Comment(0)
C
1

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#.

Convolvulus answered 17/4, 2018 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.