Tool Development 08 - Windows Command Prompt

717 views

Published on

Chapter 08 of the lecture Tool Development taught at SAE Institute Hamburg.

Introduction to the windows command prompt, and command-line arguments and calling external programs in .NET.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
717
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
30
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Tool Development 08 - Windows Command Prompt

  1. 1. Tool Development Chapter 08: Windows Command Prompt Nick Prühs
  2. 2. Assignment Solution #7 DEMO 2 / 58
  3. 3. Objectives • To understand how to interact with the Windows command prompt • To learn how to create tool chains by creating new processes in .NET 3 / 58
  4. 4. Windows Commands 4 / 58 Command Description Attrib Displays, sets, or removes attributes assigned to files or directories. Cd Displays the name of or changes the current directory. Cls Clears the Command Prompt window. Copy Copies one or more files from one location to another. Del Deletes one or more files. Dir Displays a list of a directory's files and subdirectories. Echo Displays messages or turns on or off the command echoing feature. Md Creates a directory or subdirectory. Move Moves one or more files from one directory to another directory. Rd Deletes a directory. Xcopy Copies files and directories, including subdirectories.
  5. 5. Attrib Command Displays, sets, or removes attributes assigned to files or directories. Syntax: attrib [{+|-}r] [{+|-}h] [<Drive>:][<Path>][<FileName>] [/s [/d]] 5 / 58
  6. 6. Cd Command Displays the name of or changes the current directory. Syntax: cd [<Drive>:][<Path>] cd [..] 6 / 58
  7. 7. Cls Command Clears the Command Prompt window. Syntax: cls 7 / 58
  8. 8. Copy Command Copies one or more files from one location to another. Syntax: copy [/y] <Source> [+<Source> [+ ...]] [<Destination>] 8 / 58
  9. 9. Del Command Deletes one or more files. Syntax: del [/s] <Names> 9 / 58
  10. 10. Dir Command Displays a list of a directory's files and subdirectories. Syntax: dir [<Drive>:][<Path>][<FileName>] [/p] [/a[[:]<Attributes>]] [/s] 10 / 58
  11. 11. Echo Command Displays messages or turns on or off the command echoing feature. Syntax: echo [<Message>] echo [on | off] 11 / 58
  12. 12. Md Command Creates a directory or subdirectory. Syntax: md [<Drive>:]<Path> 12 / 58
  13. 13. Move Command Moves one or more files from one directory to another directory. Syntax: move [<Source>] [<Target>] 13 / 58
  14. 14. Rd Command Deletes a directory. Syntax: rd [<Drive>:]<Path> [/s] 14 / 58
  15. 15. Xcopy Command Copies files and directories, including subdirectories. Syntax: Xcopy <Source> <Destination> [/q] [/s] [/h] 15 / 58
  16. 16. Command Prompt Wildcards Used to represent one or more characters when you are searching for files, folders, printers, computers, or people. 16 / 58 Wildcard character Description Asterisk (*) Substitute for zero or more characters. Question mark (?) Substitute for a single character in a name.
  17. 17. Windows Batch Files • Unformatted text file that contains one or more commands and has a .bat file name extension. • Allows you to simplify routine or repetitive tasks. • When you type the file name at the command prompt, Cmd.exe runs the commands sequentially as they appear in the file. 17 / 58
  18. 18. Batch File Parameters • Cmd.exe provides the batch parameter expansion variables %0 through %9. • %0 is replaced by the batch file name. • %1 through %9 are replaced by the corresponding arguments that you type at the command line. 18 / 58
  19. 19. Command Redirection Operators 19 / 58 Redirection operator Description > Writes the command output to a file or a device, such as a printer, instead of the Command Prompt window. < Reads the command input from a file, instead of reading input from the keyboard. >> Appends the command output to the end of a file without deleting the information that is already in the file. | Reads the output from one command and writes it to the input of another command. Also known as a pipe.
  20. 20. Batch File Commands 20 / 58 Command Description If Performs conditional processing in batch programs. Goto Within a batch program, redirects to a line identified by a label. Rem Enables you to include comments (remarks) in a batch file or in your configuration files.
  21. 21. If Command Performs conditional processing in batch programs. Syntax: if [not] errorlevel number command [else expression] if [not] string1==string2 command [else expression] if [not] exist FileName command [else expression] 21 / 58
  22. 22. Goto Command Within a batch program, redirects to a line identified by a label. Syntax: goto label :label 22 / 58
  23. 23. Rem Command Enables you to include comments (remarks) in a batch file or in your configuration files. Syntax: rem [comment] 23 / 58
  24. 24. Command-Line Paramters in .NET Application Type Parameter Access Console Application static void Main(string[] args) WPF Application StartupEventArgs of the Startup event handler General Environment.GetCommandLineArg s(); 24 / 58
  25. 25. Command-Line Paramters in .NET C# 25 / 58 public static void Main(string[] args) { // Print all parameters to the console. foreach (var arg in args) { Console.WriteLine(arg); } }
  26. 26. Starting Programs in .NET • Process class is a useful tool for starting, stopping, controlling, and monitoring applications • You can obtain a list of the processes that are running, or you can start a new process • After a Process has been initialized, it can be used to obtain information about the running process • set of threads • loaded modules (.dll and .exe files) • amount of memory the process is using 26 / 58
  27. 27. Starting Programs in .NET • The operating system persists the process handle, which is accessed through the Handle property of the Process component, even when the process has exited. • Thus, you can get the process's administrative information, such as the ExitCode and the ExitTime. 27 / 58
  28. 28. Starting Programs in .NET C# 28 / 58 // Create new process object. Process process = new Process(); // Set target file and command-line arguments. process.StartInfo.FileName = "ParameterDemo.exe"; process.StartInfo.Arguments = "/s"; // Start process. process.Start();
  29. 29. Starting Programs in .NET C# 29 / 58 // Create new process object. Process process = new Process(); // Set target file. Windows will automatically open the file with the default program. process.StartInfo.FileName = "textfile.txt"; // Start process. process.Start();
  30. 30. Assignment #8 1. Title Bar – Map File Name 1. Keep the file stream of the current map file open until a new map is created or opened. 2. Update the title of the main window, always showing the name of the current map file. 30 / 58
  31. 31. Assignment #8 2. Save to Current File 1. Add a Save menu item to the File menu of the Main Window, and change the Save As toolbar button to a Save toolbar button. 2. Executing the Save command should write the current map data to the current map file, if any, and open the Save As dialog otherwise. 31 / 58
  32. 32. Assignment #8 3. Title Bar – Modified Asterisk Update the title of the main window, showing an asterisk after the current map file name while the map is dirty. 32 / 58
  33. 33. References • Microsoft. Command-Line Reference. http://technet.microsoft.com/en- us/library/cc754340.aspx, November 23, 2015. • Microsoft. Using batch files. https://www.microsoft.com/resources/documentat ion/windows/xp/all/proddocs/en- us/batch.mspx?mfr=true, May 2016. • Microsoft. Using wildcard characters. http://technet.microsoft.com/en- us/library/bb490639.aspx, May 2016. 33 / 58
  34. 34. Thank you! http://www.npruehs.de https://github.com/npruehs @npruehs dev@npruehs.de
  35. 35. 5 Minute Review Session • Which wildcards are available at the windows command prompt? • How do you access parameters in batch files? • Which redirection operator writes the command output to a file or a device instead of the Command Prompt window? • How do you access command-line parameters in .NET? • How do start other programs from your .NET application? 35 / 58

×