List of valid resolutions for a given Screen? - c#

Is there a way to get ALL valid resolutions for a given screen?
I currently have a dropdown that is populated with all valid screens (using Screen.AllScreens). When the user selects a screen, I'd like to present them with a second dropdown listing all valid resolutions for that display (not just the current resolution).

I think it should be possible to get the information using Windows Management Instrumentation (WMI). WMI is accessible from .NET using the classes from them System.Management namespace.
A solution will look similar to the following. I don't know WMI well and could not immediately find the information you are looking for, but I found the WMI class for the resolutions supported by the video card. The code requires referencing System.Management.dll and importing the System.Management namespace.
var scope = new ManagementScope();
var query = new ObjectQuery("SELECT * FROM CIM_VideoControllerResolution");
using (var searcher = new ManagementObjectSearcher(scope, query))
{
var results = searcher.Get();
foreach (var result in results)
{
Console.WriteLine(
"caption={0}, description={1} resolution={2}x{3} " +
"colors={4} refresh rate={5}|{6}|{7} scan mode={8}",
result["Caption"], result["Description"],
result["HorizontalResolution"],
result["VerticalResolution"],
result["NumberOfColors"],
result["MinRefreshRate"],
result["RefreshRate"],
result["MaxRefreshRate"],
result["ScanMode"]);
}
}

The following link contains detailed code examples for this:
Task 2: Changing the Display Resolution
http://msdn.microsoft.com/en-us/library/aa719104(VS.71).aspx#docum_topic2

The accepted answer doesn't seem to work on Windows 8.1, at least on my machine. The query runs fine but there are 0 entries in the results. And considering Bijoy K Jose's comment I suppose that I am not the only one.
However the validated answer for the following question worked out just fine :
How to list available video modes using C#?
Thanks to Vimvq1987

Related

Is there a way to get a list of disabled/enabled network interfaces in C#

I am programming a little script to change the IPv4 address of a specific Wireless80211 or Ethernet network interface. So far everything is working fine. The script sets the IPv4 with the command prompt and netsh (to control it with C# I use System.Diagnostics).
I want to add the feature, that the script disables or enables all Wireless80211 and Ethernet network interfaces (without a specific one) which you can find in "Control Panel>Network and Internet>Network Connections".
The script is mainly used for ArtNet to DMX to automatically prepare everything to use ArtNet (for people, which do not know anything of the Control Panel and to automate the workflow).
I have already tried it with the System.Net.NetworkInformation namespace, but I have only found a way to get enabled network interfaces. As soon as I disable an interface System.Net.NetworkInformation does not show this interface.
I wasn't aware that NetworkInterface.GetAllNetworkInterfaces() didn't return disabled interfaces.
Anyway, you could try using the WMI api via System.Management.dll that's available in the .NET framework (you must add this reference to your project), I did a test and it allows you to interact even with disabled network interfaces.
The following example give you an idea of how to work with WMI via this api, I pretty much extracted it from the documentation:
using System;
using System.Management;
...
void ListNetworkAdapters()
{
var query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapter");
using (var searcher = new ManagementObjectSearcher(query))
{
var queryCollection = searcher.Get();
foreach (var m in queryCollection)
{
Console.WriteLine("ServiceName : {0}", m["Name"]);
Console.WriteLine("MACAddress : {0}", m["Description"]);
Console.WriteLine();
}
Console.ReadLine();
}
}
The documentation can be found here:
https://learn.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-networkadapter
NetworkInterface.GetAllNetworkInterfaces()
Then check the OperationalStatus property
https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterface?view=netframework-4.7.2

Query for retrieving account associated with ServicePrincipleName(SPN)

We have an internal site which was built on C# and has various tools for various functions to help the company like looking up/modifying/adding service accounts, for example. The tools are just C# code to automate these tasks. /vague
I've been asked to find/create a way to enter an SPN and have it return the service account which it's associated to.
So something like:
Input: HTTP/server1.company.com
Output: SVC_ACCT_AWESOME
The other request I got was for entering a service account name and have it give the KVNO back.
Issue, I have never done anything in C# before. I have Visual Studio 2013 loaded and I want to learn. I've spent hours pouring over the Googles and haven't found anything useful other than this:
https://msdn.microsoft.com/en-us/library/vstudio/system.servicemodel.configuration.identityelement.serviceprincipalname(v=vs.100).aspx
Unfortunately it doesn't mean anything to me.
Any direction that can be given for these specific tasks would be super appreciated!
Thanks.
I believe you can query AD for SPNs with something like this:
using (var root = new DirectoryEntry()) // or pass in something like "LDAP://dc=example,dc=com" to query a different domain
using (var searcher = new DirectorySearcher(root))
{
searcher.Filter = "(servicePrincipalName=HTTP/server1.company.com)";
using (var results = searcher.FindAll())
{
foreach (SearchResult result in results)
Console.WriteLine(result.Properties["samAccountName"][0]); // or whatever you want to do with it
}
}
(this needs a reference to System.DirectoryServices)

GetAllNetworkInterfaces() return an incomplete list

NetworkInterface.GetAllNetworkInterfaces() doesn't return a complete list,
when I go to A network device, and using properties I disable internet protocol version 4(TCP/IPv4) check box, GetAllNetworkInterfaces stop recognizing the device.
I find this odd as although the device is not internet capable after this(we still may have IPv6 but for the sake of the argument) its still a network device
can someone explain this, or show how this can be solved?
After some time searching I found that a complete list can be accessed by using the following:
SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject networkAdapter in searchProcedure.Get())
{
//here do whatever you want to do to the adapter
}
A more general description can be found at(not C# specific):
http://msdn.microsoft.com/en-us/library/aa394216(v=vs.85).aspx
Also in C# make sure to reference System.Management, or you wan't be able to use the code above

get list of computer names on network [duplicate]

I was wondering if there is a way to get all the computer names that show up in my network places using C#.
You will want to use the NetServerEnum() API. I dont believe there is a managed wrapper for this in the base .NET libraries but I was able to find this with a quick google search: http://www.codeproject.com/Articles/16113/Retreiving-a-list-of-network-computer-names-using
NOTE: I haven't tested or thoroughly reviewed the codeproject code but it should be enough of a starting point for what you need if there are any issues.
EDIT: Do not use DirectoryServices unless your sure of a domain environment. The System.DirectoryServices class is an ADSI wrapper that dosent work without an Active Directory to query against. NetServerEnum() works on workgroups and domains but dosen't guarantee the most reliable data (not all machines may show up). It relies on the Computer Browser service.
The best solution would probably be a class that wraps both possibilities and merges the results :/
This works, but it takes a while. :/
public List<String> ListNetworkComputers()
{
List<String> _ComputerNames = new List<String>();
String _ComputerSchema = "Computer";
System.DirectoryServices.DirectoryEntry _WinNTDirectoryEntries = new System.DirectoryServices.DirectoryEntry("WinNT:");
foreach (System.DirectoryServices.DirectoryEntry _AvailDomains in _WinNTDirectoryEntries.Children) {
foreach (System.DirectoryServices.DirectoryEntry _PCNameEntry in _AvailDomains.Children) {
if (_PCNameEntry.SchemaClassName.ToLower().Contains(_ComputerSchema.ToLower())) {
_ComputerNames.Add(_PCNameEntry.Name);
}
}
}
return _ComputerNames;
}
Depends on the user's permission, the application may or may not get those information.
Try using ActiveDirectory. This should get you precise information about the local network.
Use System.DirectoryServices.

Identifying displays

I am trying to identify a screen in a multi-monitor setup to save some data linked to that screen. How can I reliable identify a screen, also after a reboot?
I am aware of the Screen class in .Net but Screen.DeviceName seems not be consistently pointing to one screen (for example after installing a new graphics driver).
Try WMI instead of WinForms.
The following post shows how to get monitors and their details (s. DisplayDetails
class e.g.): Monitor ID and Serial Number
Edit:
My suggestion:
using System.Management;
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DesktopMonitor");
foreach (ManagementObject obj in searcher.Get())
Console.WriteLine("PNP Device ID: {0}", obj ["PNPDeviceID"]);
I solved this by getting the DeviceID with the EnumDisplayDevices API. Seems to be unique for each screen and does not change after a driver update for example.
WMI is out of the question for me as I tried it before and it only returns one monitor with my multi-monitor setup.

Categories