In this lecture, we will be executing commands via CMD, to do so we will be using process class.
If the given comment doesn't contains any of the predefined keywords, we will be sending our command
to CMD.
If you are new to these concepts, please learn more about the process class and process start operations
before keeping continue.
So let's start coding.
Inside the operations class, create a new function, I will call it ExecuteCMD, and it will take command
as argument.
And it will be returning the results of the command, so our return data type is string.
And let's create a variable for results
and initialize it as empty string and simply return it.
Inside the commandparser function, we need to create an else statement.
If the given command doesn't contain any of those predefined keywords, we will be executing executeCMD
function.
And as arguments, we will be using cmd variable's itself, because we need the whole line of the
code, so it will be command plus argument.
instead of using it like command plus arguments.
We are using cmd variable's itself.
And let's start the process.
In order to start the process, we need to create an instance of process class to do so, use
system.diagnostics namespace process classes here and create an instance of it.
I will call it pinstance.
And we need to specify the filename that our process will be using.
To specify that information, we will be using pinstance.startinfo.filename, it will be cmd.exe
and in order to execute our commands via cmd.exe we need to give arguments. To do so,
use
pInstance.startinfo.arguments and our arguments will be /c for executing commands in MSDOS mode and append our
command next to it.
Since our process wont be using the operating system shell ,we need to set UseShellExecute property
as false.
You can learn more about them by reading their descriptions or you can learn more about them by Microsoft's
official documents.
As you can see from its description, since our process will directly created from the executable file.
We set it as false.
And we want to make sure that our process won't be creating any windows, so we need to set
createn Windows property as true, so our process will the hidden.
And in order to execute our command in our current working directory, we need to set workingdirectory
as our current directory.
To get our current directory, we will be using
the instance of the generalinfo class.
Because inside the general info class, we already had that information in cdirectory variable, so
we need to access this variable.
To do so.
Simply use the instance of the generalinfo class and access the cdirectory variable.
And we want to get all of the errors and outputs, so we need to enable them
pinstance.redirect.
direct.
StartInfo, sorry about the mistake.
Redirectstandardoutput as true because we want all of the outputs and also we want to get all
of the errors to do so, set redirectstandarderror as true.
So now we can start our process by using start method of the process class.
Here it is.
So what's next, we need to get all of the outputs, an error messages to do so, we will be using
readtoend method, read all of the output and append it
to results variable.
And also we will be appending all of the errors
Now we are ready.
Let's test our code.
Inside else statement, let's try to print out the result value of the ExecuteCMD
function
by using console.writeline method. And switch back to your
program.cs
Let me clean the terminal first.
And execute the commandparser function with a command like
ipconfig /all
Let's see.
As you can see, we have successfully retrieved the network configuration from our system by executing
ipconfig /all command from CMD.
So that's it for this lecture.
See you in the next one.