zoukankan      html  css  js  c++  java
  • C# 获得一个文件的代码

    /// <summary>
    ///读取出一个文件里的代码

    /// 这个方法存在问题是:每读取一行,最后的/r/n 也就是换行的关系,每一行会丢失第一个字节。

    /// 12345 - 11111   就只会读到 2345 - 11111 
    /// </summary>
    /// <param name="path"></param>
    private string getallcode(string path)
    {
        string allcode = string.Empty;
        try
        {
             if (!File.Exists(path))
             {
                  return "没有此文件";
             }
             else
             {
                  StreamReader sr = new StreamReader(path, Encoding.GetEncoding("GB2312"));
                  byte[] cont = new byte[1000];
                  while (sr.Read() > 0)
                  {
                       allcode += sr.ReadLine() + "\r";
                  }
                  sr.Dispose();
                  sr.Close();
                  return allcode;
              }
          }
          catch
          {
                return "出错啦";
           }

    }

    -----------------------------正确的代码------------------------------------

    string path = Server.MapPath("../uploads/id.txt");
    try
    {
        StreamReader sr = new StreamReader(path, Encoding.GetEncoding("GB2312"));

        string s = string.Empty;
        while((s=sr.ReadLine())!=null)
        {
            dantiao = s;
        }

    }
    sr.Dispose();
    sr.Close();

     

  • 相关阅读:
    BZOJ4987 Tree
    BZOJ4817 [SDOI2017]树点涂色
    BZOJ4811: [YNOI2017] 由乃的OJ
    BSGS算法
    codeforces914G Sum the Fibonacci
    NOI2018网络同步赛游记
    雅礼集训 2017 Day2 水箱 可并堆
    CTSC&APIO2018游记
    51Nod 有限背包计数问题 题解报告
    CTSC2016&&APIO2016游记
  • 原文地址:https://www.cnblogs.com/binlunia/p/11267806.html
Copyright © 2011-2022 走看看