Writing Self-Replicating Code in C#

C# has some fun features under its hood, like its ability to compile an assembly from source code dynamically at run-time, allowing you to do things like write self-replicating code, code that learns (or artificial intelligence), and so forth, to whatever degree of metamorphic and polymorphic behavior you desire to get out of it.

Take this console application, for example. In each iteration, it rewrites itself under a new pseudonym and modifies its own source code every time it re-runs itself:

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace Replicator
{
    class Program
    {

        static int iteration = 0;
        static string sourcePath = "../../Program.cs";

        static void Main(string[] args)
        {
            Console.WriteLine("Iteration: {0}.", iteration);
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters(new[] { "System.dll", "System.Core.dll", "Microsoft.CSharp.dll", "System.Linq.dll" }, String.Format("Replicator{0}.exe", iteration.ToString()), true);
            parameters.GenerateExecutable = true;
            string sourceCode = File.ReadAllText(sourcePath);
            sourceCode = sourceCode.Replace(String.Format("static int iteration = {0};", iteration.ToString()), String.Format("static int iteration = {0};", (iteration + 1).ToString()));
            File.WriteAllText(sourcePath, sourceCode);
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, sourceCode);
            if (results.Errors.HasErrors)
            {
                Console.WriteLine("A clone was not created. Fix your compile errors and try running this application again at a later time. Press any key to exit.");
                results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("A clone has been created. Press any key to run it.");
                Console.ReadLine();
                Process.Start(String.Format("Replicator{0}.exe", iteration.ToString()));
            }
        }
    }
}