Reputation: 5166
I want to execute a msbuild script using a specific culture (en-US) as one of the tasks is trying to call a web service using aDateTime.ToShortDateString() as a parameter, and my current culture is uncompatible with the server's (english) format.
How to do it without altering my regional settings ?
Upvotes: 4
Views: 1324
Reputation: 5166
I ended up by creating a specific task for changing the current culture like this:
public class ChangeCulture : Task
{
[Required]
public string Culture { get; set; }
public override bool Execute()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(Culture);
return true;
}
}
Upvotes: 2
Reputation: 2488
If you want to change the culture of your entire application then you can set the culture when the application starts like this:
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
You can read about setting it on MSDN.
Given your example though, that may be overkill. If you are trying to change just the ToShortDateString() to en-US there may be a more simple way. You can use the ToString() method instead and pass in a specific format, for example you can do:
aDateTime.ToString("MM/dd/yyyy");
More specifically you can use the pre-defined culture info with System.Globalization like this:
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
string datePattern = culture.DateTimeFormat.ShortDatePattern;
string shortDate = aDateTime.ToString(datePattern);
Upvotes: -1