Set Culture in Config File?
Set Culture in Config File?
am 16.11.2007 15:48:43 von yofnik
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 setting 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
Re: Set Culture in Config File?
am 16.11.2007 16:06:40 von Marc Gravell
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
Re: Set Culture in Config File?
am 16.11.2007 16:07:22 von Marc Gravell
(sorry: dates, not date's)
Re: Set Culture in Config File?
am 16.11.2007 16:19:22 von yofnik
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.
Re: Set Culture in Config File?
am 16.11.2007 16:29:20 von Marc Gravell
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
Re: Set Culture in Config File?
am 16.11.2007 16:40:37 von yofnik
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
Re: Set Culture in Config File?
am 16.11.2007 21:25:07 von Marc Gravell
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"));
}
}
*/