so, Im trying this thing to connect to my modem and im getting a
80070005 Access is denied error
...when attempting to accessing.
Im not really sure whats the problem here because im new with this kind things. How can i possible solve this?
namespace ConsoleApp2 
{
    class Gsmsms
    {
        private SerialPort gsmPort = null;
        private bool isDeviceFound = false;
        public Gsmsms()
        {
            gsmPort = new SerialPort();
        }
        public GSMcom[] List()
        {
            List<GSMcom> gsmcom = new List<GSMcom>();
            try
            {
                ConnectionOptions option = new ConnectionOptions();
                option.Impersonation = ImpersonationLevel.Impersonate;
                option.EnablePrivileges = true;
              //  string conn = "Server=localhost;Database=election_db2;Uid=root;Pwd=;persistsecurityinfo=True;port=3306;SslMode=none";
                 string connString = $@"\\{Environment.MachineName}\root\cimv2";
                ManagementScope scope = new ManagementScope(connString, option);
                scope.Connect();
                ObjectQuery query = new ObjectQuery("Select * from Win32_POTSModem");
                ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);
                ManagementObjectCollection collection = search.Get();
                foreach (ManagementObject obj in collection)
                {
                    string portname = obj["Name"].ToString();
                    string portDescription = obj["Description"].ToString();
                    if (portname != "")
                    {
                        GSMcom com = new GSMcom();
                        com.Name = portname;
                        com.Description = portDescription;
                        gsmcom.Add(com);
                    }
                }
            }
            catch(Exception x) {
                Console.WriteLine(x);
            }
            return gsmcom.ToArray();
        }
        public bool Search()
        {
            IEnumerator enumerator = List().GetEnumerator();
            GSMcom com = enumerator.MoveNext() ? (GSMcom)enumerator.Current : null;
            if (com == null)
            {
                 isDeviceFound = false;
                Console.WriteLine("DEVICE NOT FOUND");
            }
            else
            {
                 isDeviceFound = true;
                Console.WriteLine("HUWAIE");
            }
            return isDeviceFound;
        }
    }
}
User contributions licensed under CC BY-SA 3.0