none
Getting Culture of Client machine in C#

    Pertanyaan

  • I have a server machine on which US culture is set. And my machine which is a client machine has UK culture set  on it. I want to know date format of client machine. I have written following code:

    string dateFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;

    But this is returning me culture of server machine only. I also tried to print "Thread.CurrentThread.CurrentCulture.DisplayName" value. It is shown as "English (United States)". How can I get date format of a client machine as I want it for exporting Gridview data to Excel using C#?

    • Dipindahkan oleh Kristin Xie Kamis, 02 April 2015 01.46
    Selasa, 24 Maret 2015 06.50

Semua Balasan

  • You've not explained what sort of a gridview this is so we don't know whether this is asp.net and you're doing this export on the server or it's windows forms and you're doing it on the client.... or what.

    These are significant.

    I recommend you post such questions in whichever forum is most applicable - there's a forum for each of those.  Then people know what you're asking about.

    .

    In either case I would consider using xslt to export to excel.

    If you can run code on the client machine then:

    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;

    But of course if this is say asp.net then that code would be running on the server.

    Maybe this is your issue.

    If this is web you can set UICulture="Auto" - this is a page and global setting.

    Then use

    CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture;


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    Selasa, 24 Maret 2015 09.06
  • Hi,

    maybe you can do something like this:  

    Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0]);

    Best regards.

    Selasa, 24 Maret 2015 09.17
  • I am exporting GridView data to Excel in ASP .net. My web server is having US culture while my machine is having UK culture. Can you please also explain how to set UICulture = "Auto"?
    Selasa, 24 Maret 2015 09.18
  • Here you have a full post about your problem:

    http://weblog.west-wind.com/posts/2014/Mar/27/Auto-Selecting-Cultures-for-Localization-in-ASPNET

    Give it a try..

    Selasa, 24 Maret 2015 09.23
  • Hi Cristian,

    Link which you have given does not tell how to get Client machine's Date format. It only tells about setting culture values. Thanks.

    Selasa, 24 Maret 2015 10.18
  • I am exporting GridView data to Excel in ASP .net. My web server is having US culture while my machine is having UK culture. Can you please also explain how to set UICulture = "Auto"?

    It's covered here:

    https://msdn.microsoft.com/en-us/library/bz9tc508%28v=vs.140%29.aspx?f=255&MSPPError=-2147217396

    In web.config you can add:

    <globalization uiCulture="auto" />


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    Selasa, 24 Maret 2015 10.51
  • I am exporting GridView data to Excel in ASP .net. My web server is having US culture while my machine is having UK culture. Can you please also explain how to set UICulture = "Auto"?

    Like you said, your case related to Asp.Net. If you still not resolve this issue, please repost in Asp.Net forum. you'll get more efficient responses.

    http://forums.asp.net

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Kamis, 02 April 2015 01.45
  • Hi Ajinkya,

    I have a similar issue. I am getting and saving the value in invariant culture. I only want to export the date to excel in user's culture. My application is in ASP .net Core.  What solution work for you?


    Sabtu, 10 November 2018 12.03