Tuesday, August 25, 2020

Code: Display Net Use default results with C#

 Win32_NetworkConnection


Summary

This was the WMI OS CIM class needed to get the results from a simple Windows command prompt "net use" command. 



Thoughts

This was somehow amazingly hard to find on google. For whatever reason, the only command I can find were for Win32_OperatingSystem or Win32_Share.

There is plenty of information on all the elements but just could not find a connection between net use and Win32_NetworkConnection


My Sample Code

                string serverName = @"\\serverName";
                string WMIClassName = "Win32_NetworkConnection";

                List<string> shares = new List<string>();

                ConnectionOptions connectionOptions = new ConnectionOptions();

                ManagementScope scope = new ManagementScope(serverName,
                                                            connectionOptions);
                scope.Connect();

                ManagementObjectSearcher worker = new ManagementObjectSearcher(scope,
                                   new ObjectQuery($"select Name from {WMIClassName}"));

                foreach (ManagementObject share in worker.Get())
                {
                    shares.Add(share["Name"].ToString());
                }


References

No comments:

Post a Comment