473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Set Culture in Config File?

Hello,
A C# desktop application is failing for an international customer
because dates are being parsed incorrectly. I need to set the culture
to "en-US" so the dates parse correctly. Is there any way to set the
culture information in a property setting so the application will work
for them without requiring a code change?

This is possible in ASP.NET by using the <globalizationsetting in
the Web.config file. Is there something similar that can go in the
app.config for a desktop application?

Any other suggestions for a quick resolution without requiring a
change to the code?

Thanks
Nov 16 '07 #1
6 12353
(sorry: dates, not date's)
Nov 16 '07 #2
Surely you mean "I need to correctly parse date's in my customer's
culture".

You can force the culture in property a 1-liner in code, but that is
*not* a resolution. People can get very upset having to look at
numbers "your" way.

Marc
Nov 16 '07 #3
Yes....clearly this is a (bad) oversight on our part. We certainly
need to parse the dates in their culture. But what I am looking for is
a quick resolution that does not require a code change. If that
resolution requires them to view dates in en-US format, that is ok for
the moment. It is not possible to recompile the code at this time, so
I am trying to find a workaround until they can get a new version.
Nov 16 '07 #4
A quick google failed to fine one... sorry.
Unless you change the machines local settings... rarely an option.

For info, if you need to parse an /input/ file in a different culture,
then OK (although invariant standards for data-transfer are
preferable), then you can do the following:

// in the user's culture
Trace.WriteLine(DateTime.Now);
CultureInfo old = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture =
CultureInfo.GetCultureInfo("en-us");
try { // in "en-us"
Trace.WriteLine(DateTime.Now);
} finally { // undo
Thread.CurrentThread.CurrentCulture = old;
}

Marc
Nov 16 '07 #5
Yes, thank you. That is what we will have to do to fix the problem
properly.

Unfortunately, that does not help at the moment. I could not find
anything doing a Google search either, so I turned to the message
boards. Can anyone confirm if there is a property setting that can be
changed as a quick resolution to this problem?

Thanks
Nov 16 '07 #6
OK; on the proviso that you do fix the code eventually, maybe this
will help; compile it as the same type of exe as your app (i.e.
console=>console, windows=>windows), and instead of calling *your*
exe, call this one, passing the culture ("en-us") and your exe-name as
arguments (followed by any additional arguments).

It resolves the culture, loads the assembly, locates the entry point,
switches culture and invokes the entry-point. Haven't tested every
branch, but it seems to work. You might also want to switch the ui
culture.

Marc

using System;
using System.Globalization;
using System.Reflection;
using System.IO;
using System.Threading;

class CultureShock {
static int Main(string[] args) {
try {
if (args.Length < 2) {
throw new ArgumentException("args: {culture} {exe}");
}
CultureInfo culture = CultureInfo.GetCultureInfo(args[0]);
if (culture == null) {
throw new ArgumentException("Unknown culture: " +
args[0]);
}
Assembly asm =
Assembly.LoadFile(Path.GetFullPath(args[1]));
if (asm == null) {
throw new ArgumentException("Unable to load assembly:
" + args[1]);
}

MethodInfo entry = asm.EntryPoint;
if (entry == null) {
throw new ArgumentException("No entry point in
assembly: " + asm.FullName);
}
string[] exeArgs;
ParameterInfo[] epParams = entry.GetParameters();
if (epParams.Length == 0) {
exeArgs = null;
} else if (epParams.Length == 1 &&
epParams[0].ParameterType == typeof(string[])) {
exeArgs = new string[args.Length - 2];
Array.Copy(args, 2, exeArgs, 0, exeArgs.Length);
} else {
throw new NotSupportedException("Unfamiliar entry-
point pattern");
}
object[] miArgs = {exeArgs}; // avoid array variance

// run the entry-point in the given culture
CultureInfo oldCulture =
Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = culture;
object exitCode;
try {
exitCode = entry.Invoke(null, miArgs);
} finally {
Thread.CurrentThread.CurrentCulture = oldCulture;
}
return exitCode == null ?
Environment.ExitCode : (int)exitCode;

} catch (Exception ex) {
Console.Error.WriteLine(ex.Message);
return -1;
}
}
}

/*
class Program { // my nasty test exe
static void Main(string[] args) {
Console.WriteLine(DateTime.Now.ToLongDateString()) ;
Console.WriteLine(DateTime.Now.ToLongTimeString()) ;
float f = 1234.56F;
Console.WriteLine(f.ToString("C"));
}
}
*/
Nov 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Aliasgar Pocketwala | last post by:
Hi, I have an asp.net application that is using the wrong culture. I cant figure out where does it get its setting from. My web application runs as a specific user since so that I can give it...
3
by: Ian O'Rourke | last post by:
Okay, I have a problem with the en-GB culture object My computer is set to the UK and for UK date formats, etc If I set my culture to en-GB in the web.config everything works fine with UK dates...
4
by: David Mc | last post by:
We recently installed the 1.1 redist of .Net on a new server. Only after installing .Net did I realize that the regional settings of the server had not been localized. The application we have...
1
by: simon | last post by:
I would like to set the culture for my application to English. I should change machine.config How, does anybody know? Thank you, Simon
4
by: Henke | last post by:
Hi I'm building an globalized application (english, swedish and russian languages) and have a few questions: 1. In order to see the russian characters correct I have to set the requestEncoding...
9
by: Edge | last post by:
hi, I am saving the user selected culture in a session variable so I can apply it back to all pages when refreshed and then load the proper .resx values. For that I am using global.asax
3
by: shapper | last post by:
Hello, I am working on a multilanguage Asp.Net 2.0 web site. I need to do the following: 1. Set a default culture in Web.Config File. 2. Create a function which changes culture when pressed....
3
by: Mike | last post by:
Hi I have problem as folow: Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type...
1
by: shapper | last post by:
Hello, I am initializing the culture of a page as follows: protected override void InitializeCulture() { base.InitializeCulture(); CultureInfo culture = Profile.Get().Visitor.Culture; if...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.