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.
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. 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. Attrib Command
Displays, sets, or removes attributes assigned to files
or directories.
Syntax:
attrib [{+|-}r] [{+|-}h] [<Drive>:][<Path>][<FileName>] [/s [/d]]
5 / 58
6. Cd Command
Displays the name of or changes the current
directory.
Syntax:
cd [<Drive>:][<Path>]
cd [..]
6 / 58
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. Echo Command
Displays messages or turns on or off the command
echoing feature.
Syntax:
echo [<Message>]
echo [on | off]
11 / 58
12. Md Command
Creates a directory or subdirectory.
Syntax:
md [<Drive>:]<Path>
12 / 58
13. Move Command
Moves one or more files from one directory to
another directory.
Syntax:
move [<Source>] [<Target>]
13 / 58
15. Xcopy Command
Copies files and directories, including subdirectories.
Syntax:
Xcopy <Source> <Destination> [/q] [/s] [/h]
15 / 58
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. 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. 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. 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. 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. 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. Goto Command
Within a batch program, redirects to a line identified
by a label.
Syntax:
goto label
:label
22 / 58
23. Rem Command
Enables you to include comments (remarks) in a
batch file or in your configuration files.
Syntax:
rem [comment]
23 / 58
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. 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. 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. 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. 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. 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. 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. 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. 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. 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
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