Click here to Skip to main content
15,917,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using asp.net c# , i want to change the system date format during runtime
so i find this code :
C#
RegistryKey lkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\International", true);

     
      lkey.SetValue("sShortDate", "dd/MM/yyyy");

     
      lkey.SetValue("sLongDate", "dd/MM/yyyy");


Now i face this problem :
C#
System.Security.SecurityException: Requested registry access is not allowed.


i tried to solve it using trust level, but the problem still.please any idea?
Posted
Comments
Philippe Mori 30-Dec-15 19:03pm    
Never do that. Not a good idea at all. The purpose of the security exception is exactly to prevent people like you that corrupt other people computer by doing undesired changes at global level.

1 solution

First off: don't even try.

If you start playing with the system date format you will affect a heck of a lot of other programs on the computer and annoy a lot of users. If an application tried to do that to my PC, it would be uninstalled with extreme prejudice, and I'd be looking for my money back.

If your app needs a specific date format, then provide it the formatting information when you display data, and use the appropriate format when you read date info from the user (but even then, you will likely confuse and annoy him if it isn't what he is used to on his computer - it's best to take input in whatever the user is used to).

Formatting for different cultures isn't difficult, and specifying an absolute format for dates in output is really easy: Formatting a DateTime for display - format string description[^] shows the bits you need.

But changing overall system settings? Bad idea - and that's why it's not easy to do any more!
 
Share this answer
 
Comments
loai_maane 30-Dec-15 4:20am    
thank you OriginalGriff
OriginalGriff 30-Dec-15 4:57am    
You're welcome!
loai_maane 3-Jan-16 15:00pm    
OriginalGriff,
I have one webapplication which works fine on my PC,currently i publish to server which has the database on SQL server 2012(i move the database also).now i face problem in the date format
on my PC the date format was dd/MM/yyyy but in server MM/dd/yyyy, so i got error in many places.i can not change the language for the sql server becuase it is include another database.I change Regional setting and set date Format to dd/MM/yyyy but still there is no solution, any sugestions??
OriginalGriff 4-Jan-16 4:45am    
Stop using text based dates!
Convert all dates to DateTime values and they will work transparently across all systems, because they aren't stored as "this year", "this month", etc. but instead are stored as a number of microseconds since a predefined point in time. Since that doesn't have a format, there is no need to worry about culture at all - you only use then on user input (when you can find what culture they are inputting in) and output to the user (when again, you can use the culture they prefer).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900