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:

/t:library /r:ConsoleColors.dll /out:ConsoleInputOutput.dll ConsoleInputOutput.cs

Test it by entering the following at the command prompt and then pressing the Enter key:

csc @ConsoleInputOutput.rsp

That's an even bigger savings in typing: 27 characters instead of 81 characters at the command line.

Finally, let's create a batch file that we can execute from the command line and which will rebuild both the ConsoleColors.dll and the ConsoleInputOutput.dll files:

Copy/paste the following listing into a file named rebuild.bat:

csc.exe @ConsoleColors.rsp
csc.exe @ConsoleInputOutput.rsp

Now switch to your command prompt and do a directory listing with

dir

Ensure that there is a file named rebuild.bat. Run it by entering the following at the command prompt:

rebuild

From now on, to rebuild your DLLs, you no longer have to enter this:

csc /t:library /out:ConsoleColors.dll ConsoleColors.cs then this: csc /t:library /r:ConsoleColors.dll /out:ConsoleInputOutput.dll ConsoleInputOutput.cs

Instead, you can just type rebuild at the command line.

That's 7 characters instead of 108 characters. Huzzah for response files! :D

Should you wish, you can download all the files for this lesson here.

Return to Lesson 8 | Proceed to Lesson 10

Return to Index


Author: kyrathasoft

The only programmer in Glens Fork, Ky.