How to check exists culture in .NET

Jacek picture Jacek · Dec 5, 2012 · Viewed 22.7k times · Source

I have this code, when I try to get not existed culture I get exception.
Is there exists method like TryGetCultureInfo, which return bool value? I don't want to use try-catch statement

CultureInfo culture = CultureInfo.GetCultureInfo(cultureCode);
if (culture == null)
{
    culture = CultureInfo.GetCultureInfo(DefaultCultureCode);
}

Answer

Steven S. picture Steven S. · May 10, 2013

You could write a DoesCultureExist method returning a boolean value just like this:

private static bool DoesCultureExist(string cultureName)
{
    return CultureInfo.GetCultures(CultureTypes.AllCultures).Any(culture => string.Equals(culture.Name, cultureName, StringComparison.CurrentCultureIgnoreCase));
}