• C# IO 文件读写操作


    StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + "\test.txt")
    string line;
    while ((line = sr.ReadLine()) != null)
    {
                  Console.WriteLine(line);
    }

     写操作

     string ss = "one , two , three " + "
    " + "four , fire ,sex 
     seven ,eight , nine , ten";
     string path = Directory.GetCurrentDirectory() + "//test.txt";
     FileStream fs = new FileStream(path,FileMode.Create);
     StreamWriter sw = new StreamWriter(fs);
     sw.WriteLine(ss);
     sw.Flush();

     2.InI文件读写操作

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace AppTest.config
    {
       public class InitConfigure
        {
            private static string _filePath = string.Empty;//文件路径
    
            public static string FilePath
            {
                get { return _filePath; }
                set { _filePath = value; }
            }
    
           [System.Runtime.InteropServices.DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string _filePath);
    
           [System.Runtime.InteropServices.DllImport("kernel32")]
           private static extern long WritePrivateProfileString(string section, string key, string val, string _filePath);
           
    
          public static string ReadVal(string section,string key)
          {
              string defVal ="";
              StringBuilder retVal = new StringBuilder(10000); //if the == right side have many tytes, need init the (10000),thus below have error.
              int size = 10240;
              string rt = "";
              try
              {
                  GetPrivateProfileString(section, key, defVal, retVal, size, _filePath);
                  rt = retVal.ToString();
              }
              catch
              {
                  rt ="";
              }
              return rt;
          }
    
          public static bool WriteVal(string section,string key,string val)
          {
              try
              {
                  if (WritePrivateProfileString(section, key, val, _filePath) == 0)
                      return false;
                  else
                      return true;
              }
              catch
              {
                  return false;
              }
          } 
        }
    }
  • 相关阅读:
    加密
    python数据类型
    json
    xml
    物理层
    计算机网络概述
    js之BOM概述
    消息队列介绍
    linux bond nmcli命令
    linux iostat
  • 原文地址:https://www.cnblogs.com/YuanDong1314/p/8952427.html
Copyright © 2020-2023  润新知