How do I determine which monitor my .NET Windows Forms program is running on? How do I determine which monitor my .NET Windows Forms program is running on? windows windows

How do I determine which monitor my .NET Windows Forms program is running on?


You can get an array of Screens that you have using this code.

Screen[] screens = Screen.AllScreens;

You can also figure out which screen you are on, by running this code (this is the windows form you are on)

Screen screen = Screen.FromControl(this); //this is the Form class

in short check out the Screen class and static helper methods, they might help you.

MSDN Link, doesn't have much..I suggest messing around in the code by yourself.


If you remember the window's location and size, that will be enough. When you set the position to the previously used position, if it happened to be on the second monitor it will go back there.

For example, if you have 2 monitors, both sized 1280x1024 and you set your window's left position to be 2000px, it will appear on the second monitor (assuming the second monitor is to the right of the first.) :)

If you are worried about the second monitor not being there when the application is started the next time, you can use this method to determine if your window intersects any of the screens:

private bool isWindowVisible(Rectangle rect){    foreach (Screen screen in Screen.AllScreens)    {        if (screen.Bounds.IntersectsWith(rect))            return true;    }    return false;}

Just pass in your window's desired location and it will tell you if it will be visible on one of the screens. Enjoy!


You can get the current Screen with

var s = Screen.FromControl(this);

where this is the Form (or any control on the Form). As about how to remember that is a little tricky, but I would go for the index in the Screen.AllScreens array, or maybe s.DeviceName. In either case, check before using the settings on startup, to prevent using a monitor that was disconnected.