用C#打印自己的源代码

发现自己以前写的无聊程序。拿出来发表一下。

要让一个程序能输出跟自己一样的源代码。首先,你会想到定义一个字符串,这个字符串的内容就是该源程序的代码,然后再把它输出。最关键的问题在于:输出程序代码中又有该字符串本身,如何解决这个问题呢?不多说了,看以下代码吧。

 

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文件,里面的代码跟源程序一样的哦,不信的话在把他拷贝出来编译一下。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值