发现自己以前写的无聊程序。拿出来发表一下。
要让一个程序能输出跟自己一样的源代码。首先,你会想到定义一个字符串,这个字符串的内容就是该源程序的代码,然后再把它输出。最关键的问题在于:输出程序代码中又有该字符串本身,如何解决这个问题呢?不多说了,看以下代码吧。
using System;
using System.IO;
class OutPutMyself
{
static void Main()
{
char c = (char)34; //用字符c来表示“双引号”
string str = "using System;using System.IO;class OutPutMyself{static void Main(){char c = (char)34;string str =; StreamWriter srd;srd = File.CreateText(elwin.cs); srd.WriteLine(str.Substring(0,97) + c + str + c + str.Substring(97,41) + c + str.Substring(138,8) + c + str.Substring(146)); srd.Close();}}";
StreamWriter srd;
srd = File.CreateText("elwin.cs");
srd.WriteLine(str.Substring(0,97) + c + str + c + str.Substring(97,41) + c + str.Substring(138,8) + c + str.Substring(146));
srd.Close();
}
}
产生的是一个elwin.cs文件,里面的代码跟源程序一样的哦,不信的话在把他拷贝出来编译一下。