Reputation: 67
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();
}
}
}
Upvotes: 0
Views: 5096
Reputation: 11740
Use:
switch (args[x])
{
....
}
for example
switch (args[x])
{
#region --loop
case "--loop":
x = -1;
break;
#endregion
#region --test-net
case "--test-net":
Task isAlive = Task.Factory.StartNew(() =>
{
bool alive = tlib.CheckForInternetConnection();
if (!alive)
{
while (!alive)
{
Console.WriteLine("No connectivity found.");
System.Threading.Thread.Sleep(9000);
alive = tlib.CheckForInternetConnection();
}
}
else
{
//TODO: Add alive code here
}
});
isAlive.Wait();
break;
#endregion
}
This allows you to say prog.exe --test-net and run that specific code.
--edit--
With multiple args you can string together a command, in this instance
prog.exe --test-net --loop
You can have as many args as you want. If you want to use human input for an arg you can always control the amount of args and grab args[x+1] to get the name of the person to disable/enable.
This is making an assumption that your case statements have 2 cases: --enable and --disable for instance. Then you can call the program like:
prog.exe --enable John
Upvotes: 0
Reputation: 1904
Is this in your Main? If so, you would refer to the command line arguments from the string[] args:
static void Main(string[] args)
You can see some examples here: http://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx
Upvotes: 0
Reputation: 11792
the arguments sent to your program are stored in args
static void Main(string[] args)
Upvotes: 0
Reputation: 211
Just have a look at the
string[] args
Your command-line arguments are inside the string array.
Upvotes: 0
Reputation: 94653
You may use Environment.CommandLine
to read command line argument or Environment.GetCommandLineArgs() methods.
String[] arguments = Environment.GetCommandLineArgs();
Upvotes: 0
Reputation: 540
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.Arguments = "/c ping " + machine;
processStartInfo.FileName = "cmd.exe";
Process process = new Process();
process.StartInfo = processStartInfo;
process.Start();
Here's an example using the ping command in console. You can add other options like forcing it to not open the gui etc.
Upvotes: 1