I'm using a function to return the current WiFi signal strength value given here: http://www.dotnet247.com/247reference/msgs/42/211278.aspx

When I run the code in Visual Studio 2008, I get the compile errors:

Type of namespace 'ManagementObjectSearcher'cannot be found.

I am using 'using System.Manangement'

My over all goal is to acquire the signal strength and plug it into a text box on a windows form, so once I grab the value using the function below, I simply will pop it into the box for the user to see.

Any idea on why I'm getting these errors?

Code:

 public static void signalStrentgh()
        {
           ManagementObjectSearcher searcher = new  
           ManagementObjectSearcher(@"root\WMI", "select 
           Ndis80211ReceivedSignalStrength from MSNdis_80211_ReceivedSignalStrength 
           where active=true");

            foreach (ManagementObject mo in searcher.Get())
            {
                Console.WriteLine("{0}", mo["Ndis80211ReceivedSignalStrength"]);
            }
        }

Note* Posted below is the old, incorrect version of the code, done in C++. This is in reference to the comments and help.

int GetSignalStrength()
{
  ManagementObjectSearcher *searcher = new ManagementObjectSearcher(
  "root\\WMI",
  "select Ndis80211ReceivedSignalStrength from
  MSNdis_80211_ReceivedSignalStrength where active=true");

  ManagementObjectCollection *queryCollection = searcher->Get();

  ManagementObjectCollection::ManagementObjectEnumer ator* queryEnum =
  queryCollection->GetEnumerator();
  while (queryEnum->MoveNext());

  ManagementBaseObject* object = queryEnum->get_Current();
  Object* signalStrength =
  object->GetPropertyValue(L"Ndis80211ReceivedSignalStrengt h");
  return (Convert::ToInt32(signalStrength->ToString()));
}
有帮助吗?

解决方案

To answer your question, you can't use object as a variable name. object is a keyword. Try renaming the variable to obj or o and see if that works.

For further reference, I think you're compiling c# code using some really old syntactical tricks. And I think that's throwing a lot of people off in trying to determine if this is really c#.

The pointer syntax is probably not necessary. ManagementObjectSearcher is part of the System.Management namespace and is therefore safe to use without the unsafe pointer syntax.

Also, why are you using the namespace alias qualifier (::) ? It should be safe to use the dot operator directly (ManagementObjectCollection.ManagementObjectEnumerator). In fact, it should be safe to use the dot operator everywhere here instead of the pointer syntax.

其他提示

The thing I found for C# is something like on MSDN: http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/34a66ee5-34f8-473d-b6f2-830a14e2300b/

Read carefully. It seems you are using or had adapted C++ CLR code. Your code seems to be almost right.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top