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.
objectNameis not going to benull. The more likely scenario is thatType.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:Edit: Note that
Activator.CreateInstanceetc is not the same as VBScript’sGetObject. To access that, referenceMicrosoft.VisualBasic.dll, and use: