1 00:00:00,480 --> 00:00:08,400 In this lecture, we will be executing commands via CMD, to do so we will be using process class. 2 00:00:10,050 --> 00:00:16,170 If the given comment doesn't contains any of the predefined keywords, we will be sending our command 3 00:00:16,350 --> 00:00:17,160 to CMD. 4 00:00:18,750 --> 00:00:24,930 If you are new to these concepts, please learn more about the process class and process start operations 5 00:00:25,080 --> 00:00:26,340 before keeping continue. 6 00:00:27,240 --> 00:00:28,770 So let's start coding. 7 00:00:30,440 --> 00:00:43,310 Inside the operations class, create a new function, I will call it ExecuteCMD, and it will take command 8 00:00:43,310 --> 00:00:44,150 as argument. 9 00:00:45,820 --> 00:00:52,120 And it will be returning the results of the command, so our return data type is string. 10 00:00:55,370 --> 00:00:58,160 And let's create a variable for results 11 00:01:00,950 --> 00:01:06,640 and initialize it as empty string and simply return it. 12 00:01:12,820 --> 00:01:17,380 Inside the commandparser function, we need to create an else statement. 13 00:01:19,990 --> 00:01:27,310 If the given command doesn't contain any of those predefined keywords, we will be executing executeCMD 14 00:01:27,640 --> 00:01:28,300 function. 15 00:01:30,530 --> 00:01:39,440 And as arguments, we will be using cmd variable's itself, because we need the whole line of the 16 00:01:39,440 --> 00:01:43,070 code, so it will be command plus argument. 17 00:01:45,180 --> 00:01:50,490 instead of using it like command plus arguments. 18 00:01:51,870 --> 00:01:54,170 We are using cmd variable's itself. 19 00:01:58,160 --> 00:01:59,840 And let's start the process. 20 00:02:01,030 --> 00:02:08,410 In order to start the process, we need to create an instance of process class to do so, use 21 00:02:08,410 --> 00:02:14,910 system.diagnostics namespace process classes here and create an instance of it. 22 00:02:15,040 --> 00:02:16,840 I will call it pinstance. 23 00:02:22,880 --> 00:02:27,200 And we need to specify the filename that our process will be using. 24 00:02:28,880 --> 00:02:38,420 To specify that information, we will be using pinstance.startinfo.filename, it will be cmd.exe 25 00:02:41,120 --> 00:02:48,500 and in order to execute our commands via cmd.exe we need to give arguments. To do so, 26 00:02:49,550 --> 00:02:52,280 use 27 00:02:54,260 --> 00:03:05,050 pInstance.startinfo.arguments and our arguments will be /c for executing commands in MSDOS mode and append our 28 00:03:05,060 --> 00:03:06,160 command next to it. 29 00:03:09,790 --> 00:03:20,130 Since our process wont be using the operating system shell ,we need to set UseShellExecute property 30 00:03:20,140 --> 00:03:20,730 as false. 31 00:03:22,150 --> 00:03:28,120 You can learn more about them by reading their descriptions or you can learn more about them by Microsoft's 32 00:03:28,120 --> 00:03:29,100 official documents. 33 00:03:29,530 --> 00:03:35,430 As you can see from its description, since our process will directly created from the executable file. 34 00:03:36,040 --> 00:03:37,660 We set it as false. 35 00:03:40,480 --> 00:03:48,160 And we want to make sure that our process won't be creating any windows, so we need to set 36 00:03:52,670 --> 00:03:58,420 createn Windows property as true, so our process will the hidden. 37 00:04:00,630 --> 00:04:10,800 And in order to execute our command in our current working directory, we need to set workingdirectory 38 00:04:11,520 --> 00:04:13,230 as our current directory. 39 00:04:14,730 --> 00:04:17,310 To get our current directory, we will be using 40 00:04:18,530 --> 00:04:20,660 the instance of the generalinfo class. 41 00:04:21,600 --> 00:04:29,280 Because inside the general info class, we already had that information in cdirectory variable, so 42 00:04:29,280 --> 00:04:30,780 we need to access this variable. 43 00:04:34,110 --> 00:04:34,830 To do so. 44 00:04:39,530 --> 00:04:46,460 Simply use the instance of the generalinfo class and access the cdirectory variable. 45 00:04:50,920 --> 00:05:00,370 And we want to get all of the errors and outputs, so we need to enable them 46 00:05:00,370 --> 00:05:01,090 pinstance.redirect. 47 00:05:06,140 --> 00:05:06,700 direct. 48 00:05:10,000 --> 00:05:13,140 StartInfo, sorry about the mistake. 49 00:05:14,830 --> 00:05:24,310 Redirectstandardoutput as true because we want all of the outputs and also we want to get all 50 00:05:24,310 --> 00:05:30,410 of the errors to do so, set redirectstandarderror as true. 51 00:05:32,350 --> 00:05:38,350 So now we can start our process by using start method of the process class. 52 00:05:41,740 --> 00:05:42,460 Here it is. 53 00:05:48,190 --> 00:05:58,480 So what's next, we need to get all of the outputs, an error messages to do so, we will be using 54 00:05:58,990 --> 00:06:11,260 readtoend method, read all of the output and append it 55 00:06:11,260 --> 00:06:12,810 to results variable. 56 00:06:13,480 --> 00:06:22,190 And also we will be appending all of the errors 57 00:06:28,540 --> 00:06:29,530 Now we are ready. 58 00:06:30,370 --> 00:06:31,630 Let's test our code. 59 00:06:33,510 --> 00:06:40,610 Inside else statement, let's try to print out the result value of the ExecuteCMD 60 00:06:40,630 --> 00:06:41,730 function 61 00:06:43,880 --> 00:06:50,440 by using console.writeline method. And switch back to your 62 00:06:50,490 --> 00:06:51,200 program.cs 63 00:06:52,790 --> 00:06:54,770 Let me clean the terminal first. 64 00:06:56,830 --> 00:07:06,460 And execute the commandparser function with a command like 65 00:07:07,740 --> 00:07:09,930 ipconfig /all 66 00:07:12,890 --> 00:07:13,610 Let's see. 67 00:07:21,640 --> 00:07:28,810 As you can see, we have successfully retrieved the network configuration from our system by executing 68 00:07:29,200 --> 00:07:33,070 ipconfig /all command from CMD. 69 00:07:34,570 --> 00:07:36,280 So that's it for this lecture. 70 00:07:36,670 --> 00:07:37,690 See you in the next one.