zoukankan      html  css  js  c++  java
  • 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;
              }
          } 
        }
    }
  • 相关阅读:
    SQL Server索引进阶:第十二级,创建,修改,删除
    SQL Server索引进阶第十一篇:索引碎片分析与解决
    Object.create()和new object()和{}的区别
    vue 前后端分离nginx部署
    实现组件props双向绑定解决方案
    prop不同数据类型设置默认值
    vue + element ui 阻止表单输入框回车刷新页面
    Vue.js中 watch(深度监听)的最易懂的解释
    vue-resource和axios区别
    JS中 reduce() 的用法
  • 原文地址:https://www.cnblogs.com/YuanDong1314/p/8952427.html
Copyright © 2011-2022 走看看