I have 3 wifi at my home, in some rooms I have signal from the 3 routers and If I'm connected to one windows 10 won't change to the strongest wifi. I would like to know a working way to disable and enable wifi that works in windows 10.
My code doesn't work in widnows 10.
SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
if (((string)item["NetConnectionId"]) == "Local Network Connection")
{
item.InvokeMethod("Disable", null);
}
}
I'm testing Native Wifi with some success, I'll post more results soon
Related
I am trying to detect if screen saver is running on a remote PC using WMI. The remote PC is Windows 7, but it should work for W8/W10 as well.
So far I was testing having screen saver set manually on the remote PC (via control panel) and relying on WMI Win32_Process containing SCREEN_SAVER_NAME.SCR:
ConnectionOptions options = new ConnectionOptions();
options.Username = "Username";
options.Password = "password";
ManagementScope scope =
new ManagementScope(
"\\\\10.1.1.1\\root\\cimv2", options);
scope.Connect();
while (true)
{
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (var item in queryCollection)
{
if ((string)item["Description"] == "PhotoScreensaver.scr")
{
Console.WriteLine("screensaver running");
}
}
}
This works fine when the screen saver is set via control panel on the remote PC.
However, when setting the screen saver via group policy, WMI Win32_Process no longer lists the .SCR file and my software obviously doesn't work as expected.
Any idea how to detect running screen savers, which was set using GPO?
How do I get the port details of the Bluetooth device that I have paired within my windows form c# application?
Manually I can get all port names but I need the com port name that allocated to particular Bluetooth Device.
Check this post
the Win32_PnPEntity is Plug and play devices MSDN
you can go on the drivers also to find your device
// The WMI query
const string QueryString = "SELECT * FROM Win32_PnPSignedDriver ";
SelectQuery WMIquery = new SelectQuery(QueryString);
ManagementObjectSearcher WMIqueryResults = new ManagementObjectSearcher(WMIquery);
// Make sure results were found
if (WMIqueryResults == null)
return;
// Scan query results to find port
ManagementObjectCollection MOC = WMIqueryResults.Get();
foreach (ManagementObject mo in MOC)
{
if (mo["FriendlyName"] != null && mo["FriendlyName"].ToString().Contains("YOUR_DEVICE_NAME"))
{}
//Check the mo Properties to find the COM port
}
I am using WMI query to detect USB to serial port but the problem is that in windows 7 the application takes long time to start while in windows xp it is working fine. I am using wmi query in following way
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_PnPDevice");
foreach (ManagementObject queryObj in searcher.Get())
{
if (queryObj["SameElement"].ToString().Contains("SerialPort"))
{
//do something
}
}
According to my reasoning it is happening due to large number of Pnp Devices and querying for serial port from that list. I tried using Win32_SerialPort but it working in windows xp while on my laptop(windows 7) it shows message not supported even though their are virtual serial port and USB to serial ports. It doesn't work even from administrator account. MSSerial_PortName also doesn't work on my laptop(windows7). Thus is their any way to make my application start faster in windows 7 using WMI query?
Win32_PnPDevice takes a long time
MSSerial_PortName works on my Win 7 laptop.
Try this (should work, modified from a program I had):
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
foreach (ManagementObject queryObj in searcher.Get())
{
serialPort = new SerialPort(queryObj["PortName"].ToString(), 115200, Parity.None, 8, StopBits.One);//change parameters
//If the serial port's instance name contains USB
//it must be a USB to serial device
if (queryObj["InstanceName"].ToString().Contains("USB"))//if you have a VID or PID name then this should not be nessesery
{
//should get serial to usb adapters
try
{
serialPort.Open();
if (queryObj["InstanceName"].ToString().Contains(VID_or_PID))
{
//do sth
}
serialPort.Close();
}
catch (Exception ex)
{
//exception handling
}
}
}
}
catch (ManagementException ex)
{
//write("Querying for WMI data. Exception raised(" + ex.Message + "): " + ex);
}
How can I disable/enable an internet connection? Just want to disable internet connection only not LAN.
I tried this but it is not working
string[] connections = DisconnectWrapper.Connections();
for (int i = 0; i < connections.Length; i++)
{
try
{
DisconnectWrapper.CloseConnection(connections[i]);
}
catch (Exception ex)
{ }
}
You can use WMI.
Add System.Management to your referenced and try this code
SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
if (((string)item["NetConnectionId"]) == "Local Network Connection")
{
item.InvokeMethod("Disable", null);
}
}
There is another article:Disable/Enable Network Connections Programmatically.
with WMI you can disable and enable all network connections.
Edited:
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(#"select * from Win32_NetworkAdapter"))
{
ManagementObjectCollection results = searcher.Get();
foreach (ManagementObject obj in results)
{
System.Console.WriteLine("Found adapter {0} :", obj["Caption"]);
System.Console.WriteLine("Disabling adapter ...");
object[] param = new object[0];
obj.InvokeMethod("Disable",param);
System.Console.WriteLine("Done.");
}
Console.ReadLine();
}
Be aware Some of the adapter cannot be disabled.
You can do what you need by doing a shell call to netsh.
Example:
netsh interface set interface "Target Adapter Name" enabled
netsh interface set interface "Target Adapter Name" disabled
Netsh will block so if you wait for the process you spawn to exit, you'll know it's done. It requires administrator privileges, so your process will also require it. Netsh will set its exit code to 0 on success or 1 on failure, so you can check that for feedback if needed.
I'm a c# newby who want to learn more about programming. So I started with a simple tool, wich can set the address of a choosen network adapter to static or dynamic (like the Windows TCP/IP Properties. I set the tool's design exactly like the TCP/IP Properties).
Setting a static IP address to the network adapter works pretty good.
But when I disconnet the Computer form the network and then set the network adapter's IP to dynamic (Obtain an IP address automatically"), it won't set the Windows IP Propertie to dynamic. I'm totaly lost with this problem, I have no idea how to solve it.
I understand that I only get an IP address automatically, when I'm connected to a network (where a router or a DHCP server is enabled).
But when I disconnect the computer form the network, an I set the IP Properties to dynamic with the the Windows TCP/IP Properties, it works well. What is worng in my code?
Thanks a lot for your help!
Below the code I'm using:
public void setDHCPMode()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["ipEnabled"])
continue;
try
{
string desc = (string)(mo["Description"]);
if (desc == NICcomboBox.Text)
{
ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = null;
ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null);
ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
}
toolStripStatusLabel1.Text = "DHCP set";
}
catch (Exception ex)
{
MessageBox.Show("Unable to set DHCP : " + ex.Message);
}
}
}
I found the problem. It looks like the WMI dosn't work correctly. It's impossible to set the TCP/IP Properties to DHCP, while the cable is unplugged for the specific NIC.
I solved it, through setting DHCP mode in the regestry. Now it works perfectly.