关于文件的读写

//写

string FilePath = Server.MapPath(Request.ApplicationPath)+"\\Error.Txt";//先获得该文件的实际地址
StreamWriter writer = null;

 

  if(!File.Exists(FilePath))// 检测有无文件
 {
        File.CreateText(FilePath);  //如果该文件不存在则建立
   }
   writer = File.AppendText(FilePath);//创建流
   writer.WriteLine(DateTime.Now +"@"+Error);//将文本写入
   writer.Close();//关闭

 

//读

//取得文件的实际路径
            string file_path = configpath + "\\counter.txt";

 

            if(!File.Exists(file_path))
            {
                File.CreateText(file_path);
            }

            //打开文件进行读取
            using(StreamReader srd = new StreamReader(file_path))
            {
                try
                {
                    while(srd.Peek() >= 0)//PEEK返回下一个可用字符但不使用,仅是判断
                    {
                        string str = srd.ReadLine();
                        count = UInt32.Parse(str);
                    }
                }
                catch(Exception )
                {
                    count = 0;
                }
            }
            object obj = count;
            Application["counter"] = obj;

转载于:https://www.cnblogs.com/YepQX/archive/2010/03/15/1686016.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值