I am new to C# and I am trying disable or enable users at local computer as shown in the code below. I am creating a exe and prompting users to enter username which they want to enable or disable.
Now I want to pass the arguments to a command prompt and disable or enable users. For eg:>cmd.exe John Disable.
How to pass arguments to a command prompt using c# and use the same code below to enable or disable users?
class EnableDisableUsers
{
static void Main(string[] args)
{
Console.WriteLine("Enter user account to be enabled or disabled");
string user = Console.ReadLine();
Console.WriteLine("Enter E to enable or D to disable the user account");
string enableStr = Console.ReadLine();
bool enable;
if (enableStr.Equals("E") || enableStr.Equals("e"))
{
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
// find a user
UserPrincipal username = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, user);
if (user != null)
{
try
{
//Enable User
username.Enabled = true;
username.Save();
Console.WriteLine(user + " Enabled");
}
catch (Exception e)
{
Console.WriteLine("Operation failed - Username is not valid", e.Message);
}
}
Console.ReadLine();
}
else if (enableStr.Equals("D") || enableStr.Equals("d"))
{
PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
// find a user
UserPrincipal username = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, user);
if (user != null)
{
try
{
//Disable User
username.Enabled = false;
username.Save();
Console.WriteLine(user + " Disabled");
}
catch (Exception e)
{
Console.WriteLine("Operation failed - Username is not valid", e.Message);
}
}
Console.ReadLine();
}
}
}