在控制台应用中获取屏幕分辨率

时间:2017-12-21 18:20:31

标签: c#

如何在控制台应用中获得屏幕分辨率(如果可能的话)?

Forms我可以使用:

int height = Screen.PrimaryScreen.Bounds.Height;
int width = Screen.PrimaryScreen.Bounds.Width;

但我正在寻找专门的控制方式。

因此Marc-Antoine Jutras提出了解决问题的方法。我需要int个值,所以我这样做了:

    int height = Convert.ToInt32(SystemParameters.PrimaryScreenHeight);
    int width = Convert.ToInt32(SystemParameters.PrimaryScreenWidth);

3 个答案:

答案 0 :(得分:0)

您可以在SystemParameters类中使用System.Windows命名空间,您具有以下属性:

PrimaryScreenWidth

PrimaryScreenHeight

我相信有人提出了一个回答这个问题的参考:Get and Set Screen Resolution

但是你必须将PresentationFramework.dll添加到你的控制台项目中。

using System.Windows;

namespace DispResolution
{
    class Program
    {
        static void Main(string[] args)
        {
            double height = SystemParameters.PrimaryScreenHeight;
            double Width = SystemParameters.PrimaryScreenWidth;
        }
    }
}

答案 1 :(得分:0)

WMI是一个选项;

int width;
int height;
var managementScope = new System.Management.ManagementScope();
managementScope.Connect();
var q = new System.Management.ObjectQuery("SELECT CurrentHorizontalResolution, CurrentVerticalResolution FROM Win32_VideoController");
var searcher = new System.Management.ManagementObjectSearcher(managementScope, q);
var records = searcher.Get();
foreach (var record in records)
{
    if (!int.TryParse(record.GetPropertyValue("CurrentHorizontalResolution").ToString(), out width))
    {
        throw new Exception("Throw some exception");
    }
    if (!int.TryParse(record.GetPropertyValue("CurrentVerticalResolution").ToString(), out height))
    {
        throw new Exception("Throw some exception");
    }
}
Output: 
Width:  1680
Height: 1050

答案 2 :(得分:0)

ManagementObjectSearcher mydisplayResolution = new ManagementObjectSearcher("SELECT CurrentHorizontalResolution, CurrentVerticalResolution FROM Win32_VideoController");
            foreach (ManagementObject record in mydisplayResolution.Get())
            {
                Console.WriteLine("-----------------------Current Resolution---------------------------------");
                Console.WriteLine("CurrentHorizontalResolution  -  " + record["CurrentHorizontalResolution"]);
                Console.WriteLine("CurrentVerticalResolution  -  " + record["CurrentVerticalResolution"]);

            }