Read locale settings of windows programmatically
I need to know the default page size (such as A4 or Letter) of the current locale/culture of the underlying O/S from a C# Winforms application.
I have seen a page from MSDN explaining this, but I've since lost the link. How 开发者_Go百科can I accomplish this?
i think what u need is this. not locale settings .
http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.papersize.aspx
new PrinterSettings().DefaultPageSettings.PaperSize;
You must be looking for this: http://msdn.microsoft.com/en-us/library/dd373799(v=vs.85).aspx
See this:
using 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;
}
There are many other properties of ps available which you can use.
For the lazy, here's the code that @logeeks' answer would use:
[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");
}
}
References:
- https://stackoverflow.com/a/6341920/270348
- https://msdn.microsoft.com/en-us/library/bb688130.aspx
- http://www.pinvoke.net/default.aspx/kernel32/GetLocaleInfo.html
- http://www.pinvoke.net/default.aspx/kernel32/GetLocaleInfoEx.html
精彩评论