What you want is etex's \unexpanded macro. Although, if you're using it with \thesection, are you sure this is actually what you want? For example, look at the following:
\documentclass{article}
\newwrite\tempfile
\begin{document}
\section{First}
\immediate\openout\tempfile=\jobname.tmp
\immediate\write\tempfile{\unexpanded{Section \thesection}}
\immediate\closeout\tempfile
\section{Second}
Something:
\input{\jobname.tmp}
\end{document}
What this does is write \thesection to the temporary file and then input it back in later. As you'll see if you run it, it outputs "Something: Section 2"
Is this really the behaviour you want? If you are writing "answers" then presumably, you want it to refer back to the right section where the corresponding question was, in which case you will want it to expand \thesection at the time of writing.
There may still be stuff you want unexpanded, but think about what you want expanding and what you don't...
There's a lot more information about writing to files in this question: What is the basic mechanism for writing something to an aux file?