Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
public static void DeleteUserProfile(string username)
{
    var objWmiService = Interaction.GetObject(@"Winmgmts:\\.\root\cimv2");
    var colProfiles = objWmiService.ExecQuery("select * from Win32_UserProfile");

    if (!colProfiles == null)
    {
        foreach (var objProfile in colProfiles)
        {
            if (objProfile.LocalPath.Contains(username))
            {
                try
                {
                    objProfile.Delete_();
                }
                catch (Exception ex)
                {
                }
            }
        }
    }
}




ExecQuery
error : Severity Code Description Project File Line Suppression State
Error CS1061 'object' does not contain a definition for 'ExecQuery' and no accessible extension method 'ExecQuery' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

What I have tried:

i am new on C# programming and i can't fix this please help me or give me some directions!thank you
Posted
Updated 16-Feb-20 22:46pm
Comments
F-ES Sitecore 17-Feb-20 4:41am    

Start by studying the documentation: System.Management Namespace | Microsoft Docs[^]. System Management is a somewhat advanced namespace, maybe you should start with something easier.
 
Share this answer
 
Basically, stop using var unless you need to.
When you write this:
C#
var objWmiService = Interaction.GetObject(@"Winmgmts:\\.\root\cimv2");
objWmiService is type defined by context as the return type for Interaction.GetObject which - understandably - is an object because the return value could be anything.

If you'd written it using a " proper" type name instead, you'd have got an error message which would have been a lot more specific:
C#
MyActuialType objWmiService = Interaction.GetObject(@"Winmgmts:\\.\root\cimv2");
It would have told you that you need to cast the return value to your actual type before you can access any field, properties, or methods.

So use a "proper" type, and then use as to cast it, checking the casted value for null in case it isn't what you expected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900