zoukankan      html  css  js  c++  java
  • Asp.net读取和写入txt文件方法(实例)!

    Asp.NET读取和写入txt文件方法(实例)!

    【程序第一行的引入命名空间文件 - 参考】

     System;
     using System.Collections;
     using System.Configuration;
     using System.Data;  using System.Linq;
     using System.Web;  using System.Web.Security;
     using System.Web.UI; 
     using System.Web.UI.HtmlControls;
     using System.Web.UI.WebControls;  
     using System.Web.UI.WebControls.WebParts;
     using System.Xml.Linq;  using System.IO;  

    【读取 - 参考网上的,根据需求改动了一下】

         strfile;  
           strfile = "asp.txt";  
             
           string strout;  
           strout = "";  
           if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(strfile)))  
           {  
           }  
           else  
           {  
               StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(strfile), System.Text.Encoding.Default);  
               String input = sr.ReadToEnd();  
               sr.Close();  
               strout = input;  
           }  
      
           Response.Write("ssss");  

    【写入 - 参考】

    System.IO.File.WriteAllText("e:\asp_1.txt", "I LOVE YOU!wang na");  // 一定要绝对路径  

    【读取的另一种写法】 

             system.test;  
            string txt = File.ReadAllText("d:\aaa.sys", Encoding.Default);  
            Response.Write("" + txt + ""); 

    c#asp.net 读取 写入 txt 文件

    读取TXT(已经测试过)
    public void ReadData()
        {
            //C#读取TXT文件之建立  FileStream 的对象,说白了告诉程序,     
            //文件在那里,对文件如何 处理,对文件内容采取的处理方式     
            System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
            FileStream fs = new FileStream(Server.MapPath("./wxlm/huo.txt"), FileMode.Open, FileAccess.Read);
            //仅 对文本 执行  读写操作     
            StreamReader sr = new StreamReader(fs,code);
            //定位操作点,begin 是一个参考点     
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
            //读一下,看看文件内有没有内容,为下一步循环 提供判断依据     
            //sr.ReadLine() 这里是 StreamReader的要领  可不是 console 中的~      
            string str = sr.ReadToEnd();//假如  文件有内容     
           
            //C#读取TXT文件之关上文件,留心顺序,先对文件内部执行 关上,然后才是文件~     
            sr.Close();
            fs.Close();
            Response.Write(str);
        }  
  • 相关阅读:
    Eclipse查看源码
    让你的Eclipse的智能感知也和Visual Studio 一样快
    关于The serializable class XXX does not declare a static final serialVersionUID field of type long的警告
    C#中Dictionary的用法及用途实例
    不想人工干预地自动执行存储过程?当目的表发生变动时自动执行相应的存储过程?
    再说HelloWorld
    TreeList应用(三) 收藏
    DataTable转换为List<Model>的通用类
    U盘不显示盘符
    如何在 Eclipse 中显示行号
  • 原文地址:https://www.cnblogs.com/hehehehehe/p/6096099.html
Copyright © 2011-2022 走看看