Upload
nick-pruehs
View
265
Download
0
Embed Size (px)
DESCRIPTION
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.
Citation preview
Tool DevelopmentChapter 08: Windows Command Prompt
Nick Prühs
5 Minute Review Session
• What are the main advantages of design patterns?
• Which types of design patterns do you know?
• What’s the difference between inheritance and aggregation?
• Which types of polymorphism do you know?
• What’s the difference between cohesion and coupling?
• Which design patterns can be used for implementing an undo-redo stack?
• Which parties are typically involved in drag-and-drop operations?
• How do you tell the user about the effect that the drag-and-drop operation will have on the data?
2 / 58
Assignment Solution #7
DEMO
3 / 58
Objectives
• To understand how to interact with the Windows command prompt
• To learn how to create tool chains by creating new processes in .NET
4 / 58
Windows Commands
5 / 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.
Attrib Command
Displays, sets, or removes attributes assigned to files or directories.
Syntax:
attrib [{+|-}r] [{+|-}h] [<Drive>:][<Path>][<FileName>] [/s [/d]]
6 / 58
Cd Command
Displays the name of or changes the current directory.
Syntax:
cd [<Drive>:][<Path>]cd [..]
7 / 58
Cls Command
Clears the Command Prompt window.
Syntax:
cls
8 / 58
Copy Command
Copies one or more files from one location to another.
Syntax:
copy [/y] <Source> [+<Source> [+ ...]] [<Destination>]
9 / 58
Del Command
Deletes one or more files.
Syntax:
del [/s] <Names>
10 / 58
Dir Command
Displays a list of a directory's files and subdirectories.
Syntax:
dir [<Drive>:][<Path>][<FileName>] [/p] [/a[[:]<Attributes>]] [/s]
11 / 58
Echo Command
Displays messages or turns on or off the command echoing feature.
Syntax:
echo [<Message>]echo [on | off]
12 / 58
Md Command
Creates a directory or subdirectory.
Syntax:
md [<Drive>:]<Path>
13 / 58
Move Command
Moves one or more files from one directory to another directory.
Syntax:
move [<Source>] [<Target>]
14 / 58
Rd Command
Deletes a directory.
Syntax:
rd [<Drive>:]<Path> [/s]
15 / 58
Xcopy Command
Copies files and directories, including subdirectories.
Syntax:
Xcopy <Source> <Destination> [/q] [/s] [/h]
16 / 58
Command Prompt Wildcards
Used to represent one or more characters when you are searching for files, folders, printers, computers, or people.
17 / 58
Wildcard character Description
Asterisk (*) Substitute for zero or more characters.
Question mark (?) Substitute for a single character in a name.
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.
18 / 58
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.
19 / 58
Command RedirectionOperators
20 / 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.
Batch File Commands
21 / 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.
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]
22 / 58
Goto Command
Within a batch program, redirects to a line identified by a label.
Syntax:
goto label
:label
23 / 58
Rem Command
Enables you to include comments (remarks) in a batch file or in your configuration files.
Syntax:
rem [comment]
24 / 58
Command-Line Paramtersin .NETApplication Type Parameter Access
Console Application static void Main(string[] args)
WPF Application StartupEventArgs of the Startupevent handler
General Environment.GetCommandLineArgs();
25 / 58
Command-Line Paramtersin .NETC#
26 / 58
public static void Main(string[] args)
{
// Print all parameters to the console.
foreach (var arg in args)
{
Console.WriteLine(arg);
}
}
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
27 / 58
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.
28 / 58
Starting Programs in .NET
C#
29 / 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();
Starting Programs in .NET
C#
30 / 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();
Assignment #8
1. Title Bar – Map File Name
1. Keep the file stream of the current map file open untila new map is created or opened.
2. Update the title of the main window, always showingthe name of the current map file.
31 / 58
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 currentmap data to the current map file, if any, and open theSave As dialog otherwise.
32 / 58
Assignment #8
3. Title Bar – Modified Asterisk
Update the title of the main window, showing an asteriskafter the current map file name while the map is dirty.
33 / 58
References
• Microsoft. Command-Line Reference. http://technet.microsoft.com/en-us/library/cc754340.aspx, August 15, 2012.
• Microsoft. Using batch files. http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx, May 2015.
• Microsoft. Using wildcard characters. http://technet.microsoft.com/en-us/library/bb490639.aspx, May 2015.
34 / 58
Thank you for your attention!
Contact
Blog
http://www.npruehs.de
@npruehs
Github
https://github.com/npruehs
35 / 58