In the past few days, because the client system is different, it is found that the client's region and language settings are also different, as a result, many attributes and methods that are determined based on time may encounter various strange problems during program running.
It is too time-consuming to modify the customer's region and language settings every time the program runs (no matter whether other programs have problems or not, it is most important to solve the problem of even-dropping projects first, is it really bad ). At the same time, it is found that many people are looking for methods to use C # to modify the system region and language. I will post the code and hope it will be helpful to all of you.
PS: it passes the test on XP and 2003 systems and has never tried Vista/win7. The theory is okay, because it is modified by calling the system API, just like modifying through the control panel.
Test method:
Drag a blank form, drag a button control, double-click button1 to enter the code mode of the button#click method, enter the call method.
The Code is as follows:
Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. text; <br/> using system. windows. forms; <br/> using system. runtime. interopservices; </P> <p> namespace windowsformsapplication1 <br/>{< br/> Public partial class svchost: Form <br/>{< br/> Public svchost () <br/>{< br/> initializecomponent (); <br/>}</P> <p> [dllimport ("kernel32.dll", entrypoint = "getsystemdefaultlcid")] <br/> Public static extern int getsystemdefaultlcid (); <br/> [dllimport ("kernel32.dll", entrypoint = "setlocaleinfoa")] <br/> Public static extern int setlocaleinfo (INT locale, int datetype, string datedata); <br/> Public const int locale_slongdate = 0x20; <br/> Public const int locale_s1_date = 0x1f; <br/> Public const int locale_stime = 0x1003; </P> <p> Public void datetimeformating () <br/>{< br/> try <br/> {<br/> int I = getsystemdefaultlcid (); <br/> // set the system short time format to HH: MM: SS <br/> setlocaleinfo (I, locale_stime, "HH: mm: SS "); <br/> // set the system short date format to yyyy-mm-DD <br/> setlocaleinfo (I, locale_s1_date, "yyyy-mm-dd "); <br/> // set the system long date format to yyyy-mm-DD <br/> setlocaleinfo (I, locale_slongdate, "yyyy-mm-dd "); <br/>}< br/> catch (exception ex) <br/>{< br/> MessageBox. show (ex. tostring (); <br/>}</P> <p> private void button#click (Object sender, eventargs E) <br/>{< br/> datetimeformating (); <br/>}< br/>}
Press F5 to run the program, click button1, and then open the control panel area and language to see if it has changed to the desired format.
Reprinted, please specify csdn yalan