以编程方式读取Windows的区域设置

时间:2011-06-14 09:22:26

标签: c# .net culture

我需要知道C#Winforms应用程序中底层操作系统的当前语言环境/文化的默认页面大小(例如A4或Letter)。

我看过MSDN的一个页面解释了这一点,但我已经丢失了链接。我怎么能做到这一点?

5 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

new PrinterSettings().DefaultPageSettings.PaperSize;

答案 2 :(得分:0)

见:

使用System.Drawing.Printing;

    private void button1_Click(object sender, EventArgs e)
    {

        PrintDocument doc = new PrintDocument();
        PageSettings ps = doc.DefaultPageSettings;

        if (ps.Landscape)
            label1.Text = "LANDSCAPE";
        PaperSize paperSize = ps.PaperSize;

    }

您可以使用ps的许多其他属性。

答案 3 :(得分:0)

答案 4 :(得分:0)

对于懒惰,这是@logeeks回答的代码:

[DllImport("kernel32.dll", SetLastError = true)]
static extern int GetLocaleInfo(
   uint Locale,
   uint LCType,
   [Out] StringBuilder lpLCData,
   int cchData);

public enum LCType : uint
{
    LOCALE_IPAPERSIZE = 0x0000100A,   // 1 = letter, 5 = legal, 8 = a3, 9 = a4
}

void Main()
{
    //CultureInfo culture = CultureInfo.GetCultureInfo("en-US");
    CultureInfo culture = CultureInfo.GetCultureInfo("de-DE"); ;

    var output = new StringBuilder();

    int result = GetLocaleInfo((uint)(culture.LCID), (uint)LCType.LOCALE_IPAPERSIZE, output, 99);

    if (result > 0)
    {
        // 1 = letter, 5 = legal, 8 = a3, 9 = a4
        Console.WriteLine(output.ToString());
    }
    else
    {
        Console.WriteLine("fail");
    }
}

参考文献: