1

when i try to run the code below, I am getting

"Value cannot be null. Parameter name: type"

error at runtime.

How to handle this exception and why my objectName is null here? I am expecting objectName to hold the value of local user account on my computer.

namespace Users
{
    class EnableDisableUsers
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter user account to be enabled or disabled");
            var user = Console.ReadLine();
            Console.WriteLine("Enter E to enable and D to disable the user account");
            string enableStr = Console.ReadLine();
            bool enable;
            var computer = ".";

            if (enableStr.Equals("E") || enableStr.Equals("e"))
            {
                enable = true;

                var objectName = "WinNT://" + computer + "/" + user + ",user"; 
                dynamic objUser = Activator.CreateInstance(Type.GetTypeFromProgID(objectName));
                objUser.AccountDisabled = false;
                objUser.SetInfo();
                Console.WriteLine(user + " Enabled = " + result.ToString());
                Console.ReadLine();
            }
            else if (enableStr.Equals("D") || enableStr.Equals("d"))
            {
                enable = false; 
                var objectName = "WinNT://" + computer + "/" + user + ",user";
                dynamic objUser = Activator.CreateInstance(Type.GetTypeFromProgID(objectName));
                objUser.AccountDisabled = true;
                objUser.SetInfo();
                Console.WriteLine(user + " Enabled = " + result.ToString());
                Console.ReadLine();    
            }
            else
            {
                Console.WriteLine("Operation for " + user + " failed ");
            }
        }
    }
}

Any help will be useful.

3
  • 1
    You will need to post more code than this. Where are user and computer defined? Commented Jul 17, 2012 at 8:14
  • Is Type.GetTypeFromProgID(objectName) null? Commented Jul 17, 2012 at 8:15
  • You might want to consider System.DirectoryServices for this instead. See msdn.microsoft.com/en-us/library/…. The way you are doing it is analagous to how you do it in VBScript. You can use the DirectoryEntry UserFlags property to set/unset Enabled. Commented Jul 17, 2012 at 8:19

1 Answer 1

1

How to handle this exception and why my objectName is null here?

objectName is not going to be null. The more likely scenario is that Type.GetTypeFromProgID(objectName) is returning null, because that prog-id doesn't exist, or the account doesn't have access.

Check what Type.GetTypeFromProgID(objectName) returns, and act accordingly. Make sure it is actually a prog-id, and that you are using that API correctly. For example:

var type = Type.GetTypeFromProgID(objectName);
if(type == null) throw new InvalidOperationException(
    "Invalid prog-id: " + objectName);
dynamic objUser = Activator.CreateInstance(type);

Edit: Note that Activator.CreateInstance etc is not the same as VBScript's GetObject. To access that, reference Microsoft.VisualBasic.dll, and use:

dynamic obj = Microsoft.VisualBasic.Interaction.GetObject(objectName);
Sign up to request clarification or add additional context in comments.

2 Comments

Invalid prog-id: WinNT://./John,user. This what prog-id returns. Why the user John is not being called from the group user?
@user1528803 to answer that, I'd need to see where it says that is supposed to work...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.