views:

32

answers:

2

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 ?

A: 

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);
Alexander Kahoun
"My" application is MSBuild, so I can't change it ;)
Sébastien Ros
+1  A: 

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;
    }
}
Sébastien Ros