简体   繁体   中英

Screen class doesn't get recognized by Visual Studio 2017

I have a problem with Visual Studio 2017 when I try using the screen class:

Error CS0246 The type or namespace name 'Screen' could not be found (are you missing a using directive or an assembly reference?

Error CS0234 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)

https://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApp1
{
class Program
{
    static void Main(string[] args)
    {
        Screen[] screens = Screen.AllScreens;
    }
}
}

The main problem here is you are in a console app, and calling a winforms library.

Screen.AllScreens Property

Gets an array of all displays on the system.

  • Namespace : System.Windows.Forms
  • Assembly : System.Windows.Forms (in System.Windows.Forms.dll)

The above is very important

When you have a problem like this, try and look on line for the documentation

What is says, is you need to add the assembly System.Windows.Forms to your application.

How to: Add or Remove References By Using the Add Reference Dialog Box

To add a reference in Visual C#

  • In Solution Explorer, right-click the project node and click Add Reference.

  • In the Add Reference dialog box, select the tab indicating the type of component you want to reference.

  • Select the components you want to reference, and then click OK.

It seems you don't have a reference to the System.Windows.Forms library.

  1. Go to the Project menu
  2. Select Add reference
  3. Click on Assemblies (on the left)
  4. Check System.Windows.Forms
  5. Click OK

Of course this will only work with a .NET Framework project, so if yours isn't it won't work ( System.Windows.Forms won't work).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM