C# Text Adventure: Lesson 9

Using Response and Batch Files

You may have been thinking, in Lesson 8, this is getting to be too much typing at the command line. If you felt that way, you'll like this lesson.

Let's do a little cleanup first, before we learn how to use C# Response files. Go to your heroes directory and delete the two test file executables. Then move the source code versions of the two tests into the testcode subdirectory.

Okay, on to Response files. We know that ultimately our text adventure game will depend upon ConsoleColors.dll and ConsoleInputOutput.dll, and perhaps other libraries we will create or leverage from existing sources.

Right now, if were were a bit further along in our game's development and we needed to make a change to ConsoleColors.cs, we've have to rebuild the ConsoleColors.dll library. Then, because ConsoleInputOutput.cs contains an overloaded method that depends upon two methods from ConsoleColors.dll, we'd also have to rebuild that library. Finally, we could then rebuild our game application.

That's a lot of command line typing. Response files to the rescue. By creating a file with a ".rsp" extension (which tells the C# command-line compiler to interpret it as a response file) that has the same root name as a ".cs" source code file, we can save all of the parameters — which we'd normally have to pass to csc.exe on the command line — to a file, and the compiler will automatically apply them.

Let's try an example. You know that to compile ConsoleColors.cs to a DLL library, we enter the following at the command line:

csc /t:library ConsoleColors.cs

take everything from the above line (except for csc) and save it in a file named ConsoleColors.rsp It'll look like the following screen shot when you enter it in Notepad++:

Be sure there are no spaces between /t: and library, or between /out: and ConsoleColors.dll. After saving this response file, go to your command prompt and enter the following line (making sure there is no space between @ and ConsoleColors.rsp), then press Enter:

csc @ConsoleColors.rsp

How about that!? Nice, eh? Which would you rather type,
csc /t:library /out:ConsoleColors.dll ConsoleColors.cs or csc @ConsoleColors.rsp?

Now let's make a response file for building ConsoleInputOutput.cs into a DLL library. Enter the following into a Notepad++ file that you'll save as ConsoleInputOutput.rsp: