availability of Win32_MountPoint and Win32_Volume on Windows XP? - c#

From the MSDN articles I've found -- http://msdn.microsoft.com/en-us/library/aa394515(v=VS.85).aspx -- Win32_Volume and Win32_MountPoint aren't available on Windows XP.
However, I'm developing a C# app on Windows XP (64bit), and I can get to those WMI classes just fine. Users of my app will be on Windows XP sp2 with .Net 3.5 sp1.
Googling around, I can't determine whether I can count on this or not.
Am I successful on my system because of one or more of the following:
- windows xp service pack 2?
- visual studio 2008 sp1 was installed?
- .Net 3.5 sp1?
Should I use something other than WMI to get at the volume/mountpoint info?
Below is sample code that's working...
public static Dictionary<string, NameValueCollection> GetAllVolumeDeviceIDs()
{
Dictionary<string, NameValueCollection> ret = new Dictionary<string, NameValueCollection>();
// retrieve information from Win32_Volume
try
{
using (ManagementClass volClass = new ManagementClass("Win32_Volume"))
{
using (ManagementObjectCollection mocVols = volClass.GetInstances())
{
// iterate over every volume
foreach (ManagementObject moVol in mocVols)
{
// get the volume's device ID (will be key into our dictionary)
string devId = moVol.GetPropertyValue("DeviceID").ToString();
ret.Add(devId, new NameValueCollection());
//Console.WriteLine("Vol: {0}", devId);
// for each non-null property on the Volume, add it to our NameValueCollection
foreach (PropertyData p in moVol.Properties)
{
if (p.Value == null)
continue;
ret[devId].Add(p.Name, p.Value.ToString());
//Console.WriteLine("\t{0}: {1}", p.Name, p.Value);
}
// find the mountpoints of this volume
using (ManagementObjectCollection mocMPs = moVol.GetRelationships("Win32_MountPoint"))
{
foreach (ManagementObject moMP in mocMPs)
{
// only care about adding directory
// Directory prop will be something like "Win32_Directory.Name=\"C:\\\\\""
string dir = moMP["Directory"].ToString();
// find opening/closing quotes in order to get the substring we want
int first = dir.IndexOf('"') + 1;
int last = dir.LastIndexOf('"');
string dirSubstr = dir.Substring(first , last - first);
// use GetFullPath to normalize/unescape any extra backslashes
string fullpath = Path.GetFullPath(dirSubstr);
ret[devId].Add(MOUNTPOINT_DIRS_KEY, fullpath);
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Problem retrieving Volume information from WMI. {0} - \n{1}",ex.Message,ex.StackTrace);
return ret;
}
return ret;
}

I guess the Win32_MountPoint and Win32_Volume classes are available on Windows XP Professional x64 Edition because it's based on the Windows Server 2003 codebase. On 32-bit versions of Windows XP, these classes don't exist and to perform your task you need to P/Invoke native volume management functions, like Tim said.

You may need to pinvoke into the Win32 Volume Management Functions

Related

C# Determine OS Version in .NET 6

What is the correct way to determine the correct OS version in .NET 6?
I found many different solutions regarding this topic. All of them are quite out of date I think.
I want to get something like "Windows 10 Enterprise build 22000 (64 bit)"
Environment.OSVersion.ToString()
gives "Microsoft Windows NT 10.0.22000.0"
RuntimeInformation.OSDescription
gives "Microsoft Windows 10.0.22000.0"
RuntimeInformation.OSArchitecture.ToString()
gives "X64"
So far I'm using:
Console.WriteLine(RuntimeInformation.OSDescription + " | " + RuntimeInformation.OSArchitecture.ToString())
This gives "Microsoft Windows 10.0.22000.0 | X64"
Is there a way to get something like "Windows 10 Enterprise | X64" in .NET 6?
In addition to that, I'm looking for a way to get the Windows install language and the current language.
The function:
RuntimeInformation.OSArchitecture.ToString();
returns the architecture that the OS was compiled, that is, in this case it was x86-64 (string-shaped)
The functions:
Environment.OSVersion.ToString()
RuntimeInformation.OSDescription
returns windows version
With this and with this link here:
(Get OS Version / Friendly Name in C#) we can get to this code:
using System.Runtime.InteropServices;
using System.Management;
static int GetARCHFriendlyBits(Architecture architecture)
{
return architecture switch
{
Architecture.X64 => 64,
Architecture.X86 => 32,
Architecture.Arm64 => 64,
Architecture.Arm => 32,
Architecture.Wasm => -1,
Architecture.S390x => -1,
_ => -1,
};
}
static string GetOSFriendlyName1()
{
string result = string.Empty;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
ManagementObjectSearcher searcher = new("SELECT Caption FROM Win32_OperatingSystem");
ManagementObject os = searcher.Get().Cast<ManagementObject>().First();
if (os["Caption"].ToString() is string osResult)
result = osResult;
}
else
{
return $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
}
if (result == string.Empty)
return $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
else
return $"{result} build {Environment.OSVersion.Version.Build} ({GetARCHFriendlyBits(RuntimeInformation.OSArchitecture)} bits)";
}
static string GetOSFriendlyName2()
{
string result = string.Empty;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
ManagementObjectSearcher searcher = new("SELECT Caption FROM Win32_OperatingSystem");
ManagementObject os = searcher.Get().Cast<ManagementObject>().First();
if (os["Caption"].ToString() is string osResult)
result = osResult;
}
else
{
return $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
}
if (result == string.Empty)
return $"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})";
else
return $"{result} | {RuntimeInformation.OSArchitecture}";
}
Console.WriteLine(GetOSFriendlyName1());
Console.WriteLine(GetOSFriendlyName2());
which in my case writes this line here on the console:
Microsoft Windows 11 Home Single Language build 22621 (64 bits)
Microsoft Windows 11 Home Single Language | X64
To use System.Management, I had to install microsoft NuGet System.Management

How to programmatically get information about a running/installed application in windows [duplicate]

How to get the applications installed in the system using c# code?
Iterating through the registry key "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" seems to give a comprehensive list of installed applications.
Aside from the example below, you can find a similar version to what I've done here.
This is a rough example, you'll probaby want to do something to strip out blank rows like in the 2nd link provided.
string registry_key = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using(Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
foreach(string subkey_name in key.GetSubKeyNames())
{
using(RegistryKey subkey = key.OpenSubKey(subkey_name))
{
Console.WriteLine(subkey.GetValue("DisplayName"));
}
}
}
Alternatively, you can use WMI as has been mentioned:
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach(ManagementObject mo in mos.Get())
{
Console.WriteLine(mo["Name"]);
}
But this is rather slower to execute, and I've heard it may only list programs installed under "ALLUSERS", though that may be incorrect. It also ignores the Windows components & updates, which may be handy for you.
I wanted to be able to extract a list of apps just as they appear in the start menu. Using the registry, I was getting entries that do not show up in the start menu.
I also wanted to find the exe path and to extract an icon to eventually make a nice looking launcher. Unfortunately, with the registry method this is kind of a hit and miss since my observations are that this information isn't reliably available.
My alternative is based around the shell:AppsFolder which you can access by running explorer.exe shell:appsFolder and which lists all apps, including store apps, currently installed and available through the start menu. The issue is that this is a virtual folder that can't be accessed with System.IO.Directory. Instead, you would have to use native shell32 commands. Fortunately, Microsoft published the Microsoft.WindowsAPICodePack-Shell on Nuget which is a wrapper for the aforementioned commands. Enough said, here's the code:
// GUID taken from https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
var FOLDERID_AppsFolder = new Guid("{1e87508d-89c2-42f0-8a7e-645a0f50ca58}");
ShellObject appsFolder = (ShellObject)KnownFolderHelper.FromKnownFolderId(FOLDERID_AppsFolder);
foreach (var app in (IKnownFolder)appsFolder)
{
// The friendly app name
string name = app.Name;
// The ParsingName property is the AppUserModelID
string appUserModelID = app.ParsingName; // or app.Properties.System.AppUserModel.ID
// You can even get the Jumbo icon in one shot
ImageSource icon = app.Thumbnail.ExtraLargeBitmapSource;
}
And that's all there is to it. You can also start the apps using
System.Diagnostics.Process.Start("explorer.exe", #" shell:appsFolder\" + appModelUserID);
This works for regular Win32 apps and UWP store apps. How about them apples.
Since you are interested in listing all installed apps, it is reasonable to expect that you might want to monitor for new apps or uninstalled apps as well, which you can do using the ShellObjectWatcher:
ShellObjectWatcher sow = new ShellObjectWatcher(appsFolder, false);
sow.AllEvents += (s, e) => DoWhatever();
sow.Start();
Edit: One might also be interested in knowing that the AppUserMoedlID mentioned above is the unique ID Windows uses to group windows in the taskbar.
2022: Tested in Windows 11 and still works great. Windows 11 also seems to cache apps that aren't installed per se, portable apps that don't need installing, for example. They appear in the start menu search results and can also be retrieved from shell:appsFolder as well.
I agree that enumerating through the registry key is the best way.
Note, however, that the key given, #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", will list all applications in a 32-bit Windows installation, and 64-bit applications in a Windows 64-bit installation.
In order to also see 32-bit applications installed on a Windows 64-bit installation, you would also need to enumeration the key #"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall".
You can take a look at this article. It makes use of registry to read the list of installed applications.
public void GetInstalledApps()
{
string uninstallKey = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(uninstallKey))
{
foreach (string skName in rk.GetSubKeyNames())
{
using (RegistryKey sk = rk.OpenSubKey(skName))
{
try
{
lstInstalled.Items.Add(sk.GetValue("DisplayName"));
}
catch (Exception ex)
{ }
}
}
}
}
While the accepted solution works, it is not complete. By far.
If you want to get all the keys, you need to take into consideration 2 more things:
x86 & x64 applications do not have access to the same registry.
Basically x86 cannot normally access x64 registry. And some
applications only register to the x64 registry.
and
some applications actually install into the CurrentUser registry instead of the LocalMachine
With that in mind, I managed to get ALL installed applications using the following code, WITHOUT using WMI
Here is the code:
List<string> installs = new List<string>();
List<string> keys = new List<string>() {
#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
#"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
};
// The RegistryView.Registry64 forces the application to open the registry as x64 even if the application is compiled as x86
FindInstalls(RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64), keys, installs);
FindInstalls(RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64), keys, installs);
installs = installs.Where(s => !string.IsNullOrWhiteSpace(s)).Distinct().ToList();
installs.Sort(); // The list of ALL installed applications
private void FindInstalls(RegistryKey regKey, List<string> keys, List<string> installed)
{
foreach (string key in keys)
{
using (RegistryKey rk = regKey.OpenSubKey(key))
{
if (rk == null)
{
continue;
}
foreach (string skName in rk.GetSubKeyNames())
{
using (RegistryKey sk = rk.OpenSubKey(skName))
{
try
{
installed.Add(Convert.ToString(sk.GetValue("DisplayName")));
}
catch (Exception ex)
{ }
}
}
}
}
}
it's worth noting that the Win32_Product WMI class represents products as they are installed by Windows Installer. not every application use windows installer
however "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" represents applications for 32 bit. For 64 bit you also need to traverse "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" and since not every software has a 64 bit version the total applications installed are a union of keys on both locations that have "UninstallString" Value with them.
but the best options remains the same .traverse registry keys is a better approach since every application have an entry in registry[including the ones in Windows Installer].however the registry method is insecure as if anyone removes the corresponding key then you will not know the Application entry.On the contrary Altering the HKEY_Classes_ROOT\Installers is more tricky as it is linked with licensing issues such as Microsoft office or other products.
for more robust solution you can always combine registry alternative with the WMI.
string[] registryKeys = new string[] {
#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
#"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" };
public class InstalledApp
{
public string DisplayName { get; set; }
public string DisplayIcon { get; set; }
public string Version { get; set; }
public string InstallLocation { get; set; }
}
private void AddInstalledAppToResultView(RegistryHive hive, RegistryView view, string registryKey,Dictionary<string,InstalledApp> resultView)
{
using (var key = RegistryKey.OpenBaseKey(hive, view).OpenSubKey(registryKey))
{
foreach (string subKeyName in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subKeyName))
{
var displayName = subkey.GetValue("DisplayName");
var displayIcon = subkey.GetValue("DisplayIcon");
if (displayName == null || displayIcon == null)
continue;
var app = new InstalledApp
{
DisplayName = (string)displayName,
DisplayIcon = (string)displayIcon,
InstallLocation = (string)subkey.GetValue("InstallLocation"),
Version = (string)subkey.GetValue("DisplayVersion")
};
if(!resultView.ContainsKey(app.DisplayName))
{
resultView.Add(app.DisplayName,app);
}
}
}
}
}
void Main()
{
var result = new Dictionary<string,InstalledApp>();
var view = Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32;
AddInstalledAppToResultView(RegistryHive.LocalMachine, view, registryKeys[0],result);
AddInstalledAppToResultView(RegistryHive.CurrentUser, view, registryKeys[0],result);
AddInstalledAppToResultView(RegistryHive.LocalMachine, RegistryView.Registry64, registryKeys[1],result);
Console.WriteLine("==============" + result.Count + "=================");
result.Values.ToList().ForEach(item => Console.WriteLine(item));
}
Iterate through "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" keys and check their "DisplayName" values.
Use Windows Installer API!
It allows to make reliable enumeration of all programs. Registry is not reliable, but WMI is heavyweight.
The object for the list:
public class InstalledProgram
{
public string DisplayName { get; set; }
public string Version { get; set; }
public string InstalledDate { get; set; }
public string Publisher { get; set; }
public string UnninstallCommand { get; set; }
public string ModifyPath { get; set; }
}
The call for creating the list:
List<InstalledProgram> installedprograms = new List<InstalledProgram>();
string registry_key = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
using (RegistryKey subkey = key.OpenSubKey(subkey_name))
{
if (subkey.GetValue("DisplayName") != null)
{
installedprograms.Add(new InstalledProgram
{
DisplayName = (string)subkey.GetValue("DisplayName"),
Version = (string)subkey.GetValue("DisplayVersion"),
InstalledDate = (string)subkey.GetValue("InstallDate"),
Publisher = (string)subkey.GetValue("Publisher"),
UnninstallCommand = (string)subkey.GetValue("UninstallString"),
ModifyPath = (string)subkey.GetValue("ModifyPath")
});
}
}
}
}
As others have pointed out, the accepted answer does not return both x86 and x64 installs. Below is my solution for that. It creates a StringBuilder, appends the registry values to it (with formatting), and writes its output to a text file:
const string FORMAT = "{0,-100} {1,-20} {2,-30} {3,-8}\n";
private void LogInstalledSoftware()
{
var line = string.Format(FORMAT, "DisplayName", "Version", "Publisher", "InstallDate");
line += string.Format(FORMAT, "-----------", "-------", "---------", "-----------");
var sb = new StringBuilder(line, 100000);
ReadRegistryUninstall(ref sb, RegistryView.Registry32);
sb.Append($"\n[64 bit section]\n\n{line}");
ReadRegistryUninstall(ref sb, RegistryView.Registry64);
File.WriteAllText(#"c:\temp\log.txt", sb.ToString());
}
private static void ReadRegistryUninstall(ref StringBuilder sb, RegistryView view)
{
const string REGISTRY_KEY = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, view);
using var subKey = baseKey.OpenSubKey(REGISTRY_KEY);
foreach (string subkey_name in subKey.GetSubKeyNames())
{
using RegistryKey key = subKey.OpenSubKey(subkey_name);
if (!string.IsNullOrEmpty(key.GetValue("DisplayName") as string))
{
var line = string.Format(FORMAT,
key.GetValue("DisplayName"),
key.GetValue("DisplayVersion"),
key.GetValue("Publisher"),
key.GetValue("InstallDate"));
sb.Append(line);
}
key.Close();
}
subKey.Close();
baseKey.Close();
}
Your best bet is to use WMI. Specifically the Win32_Product class.
Might I suggest you take a look at WMI (Windows Management Instrumentation).
If you add the System.Management reference to your C# project, you'll gain access to the class `ManagementObjectSearcher', which you will probably find useful.
There are various WMI Classes for Installed Applications, but if it was installed with Windows Installer, then the Win32_Product class is probably best suited to you.
ManagementObjectSearcher s = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
I used Nicks approach - I needed to check whether the Remote Tools for Visual Studio are installed or not, it seems a bit slow, but in a seperate thread this is fine for me. - here my extended code:
private bool isRdInstalled() {
ManagementObjectSearcher p = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
foreach (ManagementObject program in p.Get()) {
if (program != null && program.GetPropertyValue("Name") != null && program.GetPropertyValue("Name").ToString().Contains("Microsoft Visual Studio 2012 Remote Debugger")) {
return true;
}
if (program != null && program.GetPropertyValue("Name") != null) {
Trace.WriteLine(program.GetPropertyValue("Name"));
}
}
return false;
}
My requirement is to check if specific software is installed in my system. This solution works as expected. It might help you. I used a windows application in c# with visual studio 2015.
private void Form1_Load(object sender, EventArgs e)
{
object line;
string softwareinstallpath = string.Empty;
string registry_key = #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (var baseKey = Microsoft.Win32.RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (var key = baseKey.OpenSubKey(registry_key))
{
foreach (string subkey_name in key.GetSubKeyNames())
{
using (var subKey = key.OpenSubKey(subkey_name))
{
line = subKey.GetValue("DisplayName");
if (line != null && (line.ToString().ToUpper().Contains("SPARK")))
{
softwareinstallpath = subKey.GetValue("InstallLocation").ToString();
listBox1.Items.Add(subKey.GetValue("InstallLocation"));
break;
}
}
}
}
}
if(softwareinstallpath.Equals(string.Empty))
{
MessageBox.Show("The Mirth connect software not installed in this system.")
}
string targetPath = softwareinstallpath + #"\custom-lib\";
string[] files = System.IO.Directory.GetFiles(#"D:\BaseFiles");
// Copy the files and overwrite destination files if they already exist.
foreach (var item in files)
{
string srcfilepath = item;
string fileName = System.IO.Path.GetFileName(item);
System.IO.File.Copy(srcfilepath, targetPath + fileName, true);
}
return;
}

In what versions of Windows are Storage Management API Classes such as 'MSFT_PhysicalDisk' implemented?

I am trying pull metrics such as 'MediaType' from MSFT_PhysicalDisk. I'm successful on a Windows 10 machine, but not on a Windows 7 machine.
On what type of machines is MSFT_PhysicalDisk available?
The reference for Storage Management API Classes:
https://learn.microsoft.com/en-us/previous-versions/windows/desktop/stormgmt/storage-management-api-classes
See code below for an example of what I'm trying to do:
bool isSsd;
try
{
var physDiskQuery =
$"SELECT MediaType FROM MSFT_PhysicalDisk WHERE DeviceID='{driveNumber.Value}'";
var wmiScope = #"\\.\root\microsoft\windows\storage";
using (var physicalDiskSearcher = new ManagementObjectSearcher(wmiScope, physDiskQuery))
{
var objectCollection = physicalDiskSearcher.Get();
var physicalDisk = objectCollection.Cast<ManagementBaseObject>().SingleOrDefault();
if (physicalDisk == null)
return null;
isSsd = (ushort)physicalDisk["MediaType"] == 4;
}
}
catch (Exception exception)
{
Debug.WriteLine($"Error while checking for SSD drive. Details: {exception.GetBaseException()}");
return null;
}
return isSsd;
MSDN documentation lists requirements way at the bottom of the page. For the MSFT_PhysicalDisk class it says...
Minimum supported client: Windows 8 [desktop apps only]
Minimum supported server: Windows Server 2012 [desktop apps only]
In other words, you need at least Windows version 6.2.

check whether microsoft components are present or not

I want to check whether certain microsoft components like wmencoder, directx or wmplayer
are installed or not. If it is installed, can I also get its version number?
How can I do that?
Thanks in advance.
I use the below to determine if other applications are installed, however you will need to know the "unique" product code (from the setup project in Visual Studio) that the application is installed with in the registry.
Include
using System.Diagnostics;
using Microsoft.Win32;
Usage:
// HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{0006F03A-0000-0000-C000-000000000046} << This is outlook 2003
String retval = "";
// Look to see if Outlook 2003 is installed and if it is...
if ((checkComServerExists("{0006F03A-0000-0000-C000-000000000046}", out retval)))
{
// Update boolean flag if we get this far so we don't have to check again
Console.WriteLine("Office CSLID exists - Version: " + retval);
}
Function:
// Checks to see if the given CLSID is registerd and exists on the system
private static Boolean checkComServerExists(String CLSID, out String retval)
{
RegistryKey myRegKey = Registry.LocalMachine;
Object val;
try
{
// get the pathname to the COM server DLL/EXE if the key exists
myRegKey = myRegKey.OpenSubKey("SOFTWARE\\Classes\\CLSID\\" + CLSID + "\\LocalServer32");
val = myRegKey.GetValue(null); // the null gets default
}
catch
{
retval = "CLSID not registered";
return false;
}
FileVersionInfo myFileVersionInfo = null;
try
{
// parse out the version number embedded in the resource
// in the DLL
myFileVersionInfo = FileVersionInfo.GetVersionInfo(val.ToString());
}
catch
{
retval = String.Format("DLL {0} not found", val.ToString());
return false;
}
retval = myFileVersionInfo.FileVersion;
return true;
}
My first thought would be WMI. Class Win32_SoftwareElement (on MSDN)
But likely to take some work to get the right classes and queries. Start with the WMI tools for WMI CIM Studio.
Using PowerShell, something like:
gwmi win32_softwareelement -filter "name like '%play%'" | ft
will allow finding the right ids. (Warning: this is extremely slow.)
Possible that the MS Installer (MSI) API has something quicker.
I use RegShot to determine registry setting that can be used to check if a softwre is installed ..
Here is also a small code snippet that uses, among others, Type.GetTypeFromProgID and registry access.

Get SSID of the wireless network I am connected to with C# .Net on Windows Vista

I'd like to know if there is any .Net class that allows me to know the SSID of the wireless network I'm connected to.
So far I only found the library linked below. Is the best I can get or should I use something else?
Managed WiFi (http://www.codeplex.com/managedwifi)
The method that exploits WMI works for Windows XP but is it not working anymore with Windows Vista.
I resolved using the library. It resulted to be quite easy to work with the classes provided:
First I had to create a WlanClient object
wlan = new WlanClient();
And then I can get the list of the SSIDs the PC is connected to with this code:
Collection<String> connectedSsids = new Collection<string>();
foreach (WlanClient.WlanInterface wlanInterface in wlan.Interfaces)
{
Wlan.Dot11Ssid ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;
connectedSsids.Add(new String(Encoding.ASCII.GetChars(ssid.SSID,0, (int)ssid.SSIDLength)));
}
We were using the managed wifi library, but it throws exceptions if the network is disconnected during a query.
Try:
var process = new Process
{
StartInfo =
{
FileName = "netsh.exe",
Arguments = "wlan show interfaces",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
var output = process.StandardOutput.ReadToEnd();
var line = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault(l => l.Contains("SSID") && !l.Contains("BSSID"));
if (line == null)
{
return string.Empty;
}
var ssid = line.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries)[1].TrimStart();
return ssid;
It looks like this will do what you want:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI",
"SELECT * FROM MSNdis_80211_ServiceSetIdentifier");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("MSNdis_80211_ServiceSetIdentifier instance");
Console.WriteLine("-----------------------------------");
if(queryObj["Ndis80211SsId"] == null)
Console.WriteLine("Ndis80211SsId: {0}",queryObj["Ndis80211SsId"]);
else
{
Byte[] arrNdis80211SsId = (Byte[])
(queryObj["Ndis80211SsId"]);
foreach (Byte arrValue in arrNdis80211SsId)
{
Console.WriteLine("Ndis80211SsId: {0}", arrValue);
}
}
}
from http://bytes.com/groups/net-c/657473-wmi-wifi-discovery
there is some more information in How do I get the available wifi APs and their signal strength in .net?
(cross-posted in How to get currently connected wifi SSID in c# using WMI or System.Net.NetworkInformation windows 10?)
I found a rather old library dating back to 2014:
Microsoft.WindowsAPICodePack-Core version 1.1.0.2
Although it is not conforming to .NET Standard, this library integrates with my .NET Core 3.0 app, but obviously is not cross-platform.
Sample code:
var networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected);
foreach (var network in networks) {
sConnected = ((network.IsConnected == true) ? " (connected)" : " (disconnected)");
Console.WriteLine("Network : " + network.Name + " - Category : " + network.Category.ToString() + sConnected);
}
You are going to have to use native WLAN API. There is a long discussion about it here. Apparently this is what Managed Wifi API uses, so it will be easier for you to use it if you do not have any restrictions to use LGPL code.
I wanted to do exactly this, and tried using ManagedWifi, as suggested in other answers. But that led to unresolvable Exceptions as per here:
Issues with using Managed WiFi (NativeWiFi API)
I solved this by switching to using SimpleWiFi entirely and ignored the ManagedWifi package.
Glancing at the source code, it looks like SW is a fixed reimplementation of some of the functionality in MW.

Categories