0
Follow
0
View

c# operates Windows10

a112780070 注册会员
2023-02-28 12:14

Create a new winfrom program and execute the following code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1 {
    public partial class svchost: Form {
            public svchost() {
                InitializeComponent();
            }[DllImport("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")] 
            public static extern int GetSystemDefaultLCID();
            [DllImport("kernel32.dll", EntryPoint = "SetLocaleInfoA")] public static extern int SetLocaleInfo(int Locale, int dateType, string dateData);
            public const int LOCALE_SLONGDATE = 0x20;
            public const int LOCALE_SSHORTDATE = 0x1F;
            public const int LOCALE_STIME = 0x1003;
            public void datetimeformating() {
                    try {
                        int i = GetSystemDefaultLCID(); //设置系统短时间格式为HH:mm:ss 
                        SetLocaleInfo(i, LOCALE_STIME, "HH:mm:ss"); //设置系统短日期格式为yyyy-MM-dd 
                        SetLocaleInfo(i, LOCALE_SSHORTDATE, "yyyy-MM-dd"); //设置系统长日期格式为yyyy-MM-dd 
                        SetLocaleInfo(i, LOCALE_SLONGDATE, "yyyy-MM-dd"); } 
                    catch (Exception ex)
                    { 
                        MessageBox.Show(ex.ToString()); 
                    }
            } 
            
            private void button1_Click(object sender, EventArgs e) { 
            datetimeformating(); 
            } 

    } 
}
dbwjik 注册会员
2023-02-28 12:14
crd0605 注册会员
2023-02-28 12:14

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > to use c # write a program to switch Windows 10 regional language to English(United States), You can use Windows API functions to accomplish this task. Specifically, you can use the following three API functions:

1. SetThreadLocale() : Sets the locale of the current thread to the specified locale. You can use this function to set the region to the United States.

2. SetSystemDefaultLocale() : sets the default locale of the system to the specified locale. Use this function to ensure that the locale is set to the United States for all new threads.

3. SendMessageTimeout() : Send a message to the specified window and wait for a response. Use this function to send a message to the Region window to update and display the current region Settings.

Here is an example code to set the regional language to English(US) in Windows 10:

using System;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool SetThreadLocale(uint Locale);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool SetSystemDefaultLocale(uint Locale);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);

    public static void Main()
    {
        uint locale = 0x0409; // 美国英语的本地标识符为0x0409
        SetThreadLocale(locale);
        SetSystemDefaultLocale(locale);

        IntPtr regionWindow = FindWindow("Win32Window", "Region"); // 找到“Region”窗口
        const uint WM_COMMAND = 0x0111; // 发送WM_COMMAND消息
        const uint BN_CLICKED = 0x00F5; // 单击按钮的命令代码
        const uint TIMEOUT = 5000; // 超时时间为5秒

        if (regionWindow != IntPtr.Zero)
        {
            // 发送“单击按钮”消息
            SendMessageTimeout(regionWindow, WM_COMMAND, new UIntPtr(BN_CLICKED << 16 | 1), null, 0, TIMEOUT, out UIntPtr _);
        }
    }
}

This code uses SetThreadLocale() and SetSystemDefaultLocale() to set the locale language for the current thread and system to American English. Then, use the FindWindow() function to find a window named "Region" and use the SendMessageTimeout() function to send a button-click message to the window to update the display region Settings.

Note that this code requires administrator permissions to run, because setting the system default region requires administrator permissions.

cszhangjinbo 注册会员
2023-02-28 12:14

This answer quotes ChatGPT

You can change the regional language of Windows 10 to English(US) using the following code:


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;

namespace LanguageSwitcher
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                CultureInfo cultureInfo = new CultureInfo("en-US");
                ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_OperatingSystem");
                foreach (ManagementObject mo in mos.Get())
                {
                    object[] parameters = { cultureInfo.Name };
                    object result = mo.InvokeMethod("Win32_ChangeTimeZone", parameters);
                    Console.WriteLine("Windows language changed to English (United States).");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

This code uses the ManagementObjectSearcher class to query the Win32_OperatingSystem WMI class, which provides information about the operating system, including locale information. It then calls the Win32_ChangeTimeZone method to change the Windows zone language to English(US).

Note that this code requires administrator privileges to run successfully. If you want to implement this functionality in your application, you need to run the program as an administrator or use UAC(User Account Control) to request administrator rights.

cuihongxiangvic 注册会员
2023-02-28 12:14

Using GPT and your own ideas, here is sample code for using C# to switch the locale language to English(US) :

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace ChangeSystemLocale
{
    class Program
    {
        // 设置系统区域语言为英语(美国)
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool SetSystemDefaultLocale(string localeName);

        static void Main(string[] args)
        {
            // 获取英语(美国)区域语言的LCID
            int lcid = new CultureInfo("en-US").LCID;

            // 枚举系统区域语言设置
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"))
            {
                foreach (ManagementObject os in searcher.Get())
                {
                    // 获取系统区域语言设置信息
                    string locale = os["OSLocale"].ToString();
                    string systemLocale = os["SystemLocale"].ToString();
                    string userLocale = os["UserLocale"].ToString();

                    // 判断当前系统区域语言是否为英语(美国)
                    if (locale.Equals("0409"))
                    {
                        Console.WriteLine("当前系统区域语言已设置为英语(美国)!");
                    }
                    else
                    {
                        Console.WriteLine("当前系统区域语言为 {0},正在切换为英语(美国)...", locale);

                        // 设置系统区域语言为英语(美国)
                        if (SetSystemDefaultLocale("en-US"))
                        {
                            Console.WriteLine("系统区域语言设置成功!");
                        }
                        else
                        {
                            Console.WriteLine("系统区域语言设置失败!");
                        }
                    }
                }
            }

            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
        }
    }
}

The sample code uses the DllImport feature of C# to call the Windows API function SetSystemDefaultLocale to set the system locale language to English(US). When in use, run the program directly to switch the system area language to English(United States).

Note: This example code is only for Windows operating systems and needs to be run with administrator privileges. In the actual application, it may need to be modified and optimized according to the specific situation.

About the Author

Question Info

Publish Time
2023-02-28 12:14
Update Time
2023-02-28 12:14

Related Question

尝试在pyspark中使用collect()方法时出错.(windows10)[副本]

Windows10更新失败

无法在windows10 /Visual Studio 2022上构建增强

windows10上卸载了IDM后网卡驱动消失且无法修复,如何解决?

windows10 内置administrator用户和普通账户区别?

在windows10下如何在cuda上调用open3d

Windows10开机速度变慢?

Windows10如何用蓝牙连接并远程控制手机?

电脑误安装微pe工具如何恢复windows10

Windows10重启只显示背景色不显示锁屏壁纸