• C#文件操作


    try
    {
    string directory = System.AppDomain.CurrentDomain.BaseDirectory + "logs";
    string fileName = System.AppDomain.CurrentDomain.BaseDirectory + "logs\amount.txt";

    if (File.Exists(fileName))
    {
    //读取
    StreamReader m_streamReader = new StreamReader(fileName, Encoding.GetEncoding("GB2312"), true);
    m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
    int[] amount = new int[2];
    string strLine;
    for (int i = 0; i < 2; i++)
    {
    strLine = m_streamReader.ReadLine();
    if (strLine != null)
    {
    amount[i] = Int32.Parse(strLine.Split(':')[1]);
    Console.WriteLine(amount[i]);
    }
    }
    m_streamReader.Close();
    m_streamReader.Dispose();

    amount[rightOrWrong]++;

    //写入
    FileStream fs = new FileStream(fileName, FileMode.Create);
    StreamWriter sw = new StreamWriter(fs);
    //开始写入
    sw.WriteLine("right:" + amount[0]);
    sw.WriteLine("wrong:" + amount[1]);
    //清空缓冲区
    sw.Flush();
    //关闭流
    sw.Close();
    fs.Close();
    }
    else
    {
    if (!Directory.Exists(directory))
    {
    Directory.CreateDirectory(directory);
    }
    FileStream fs = new FileStream(fileName, FileMode.Create);
    StreamWriter sw = new StreamWriter(fs);
    //开始写入
    sw.WriteLine("right:1");
    sw.WriteLine("wrong:1");
    //清空缓冲区
    sw.Flush();
    //关闭流
    sw.Close();
    fs.Close();
    }

    //Console.ReadKey();

    //fs.Close();
    //fs.Dispose();
    }
    catch (Exception ex)
    {
    LogHelper2.Exception(ex.Message);
    throw;
    }

  • 相关阅读:
    iOS controller 和 window 图层
    iOS CGAffineTransform 仿射变换
    iOS UIButton的UIEdgeInsets
    iOS UI的动态布局
    iOS 栅格动态布局
    iOS 系统键盘几个类型
    iOS 金融类高精度处理
    Sublime Text (崇高文本)
    iOS 静态库——制作bundle
    iOS 静态库——制作Framework
  • 原文地址:https://www.cnblogs.com/lcyuhe/p/4878378.html
Copyright © 2020-2023  润新知