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; } } } }