Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在控制台应用程序中获取屏幕分辨率_C# - Fatal编程技术网

C# 在控制台应用程序中获取屏幕分辨率

C# 在控制台应用程序中获取屏幕分辨率,c#,C#,如何在控制台应用程序中获得屏幕分辨率(如果可能) 在表单中我可以使用: int height = Screen.PrimaryScreen.Bounds.Height; int width = Screen.PrimaryScreen.Bounds.Width; 但我是从另一个角度看的 因此,解决我的问题的方法是由。我需要int值,所以我这样做: int height = Convert.ToInt32(SystemParameters.PrimaryScreenHeight);

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

表单中
我可以使用:

int height = Screen.PrimaryScreen.Bounds.Height;
int width = Screen.PrimaryScreen.Bounds.Width;
但我是从另一个角度看的


因此,解决我的问题的方法是由。我需要
int
值,所以我这样做:

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

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

我相信有人在回答这个问题时提到:

但您必须将PresentationFramework.dll添加到控制台项目中

using System.Windows;

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

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

对于控制台应用程序,没有具体的方法可以做到这一点,因为控制台毕竟只是一个窗口;您可以引用
System.Windows.Forms
并使用与以前相同的代码。@Clint否。您可以使用。刚刚检查过,效果很好。要使用答案中的代码片段,请添加
include System.Drawing;使用System.Runtime.InteropServices@Sergey.quixoticaxis.Ivanov是的。但是给猫剥皮有很多方法。这不是一种“特定于控制台的方式”。当然,你可以通过p/invoke来获得它,但为什么要在添加引用时费心呢?@Clint因为PresentationFoundation和表单都很重,而且都会带来一堆依赖性。如果另一个问题的答案解决了这个问题,请将这个问题标记为重复的,而不是发布答案。这不起作用,因为DPI设置为125%,宽度和高度变成(1536864)而不是(19201080)我在Windows10中为自己工作过。我有多个监视器,但它似乎只返回其中一个监视器的分辨率。
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"]);

            }