打開文件並寫入:
void CreateOrOPenFile(string path, string name, string info){ //路徑、文件名、寫入內容
StreamWriter sw;
FileInfo fi = new FileInfo(path + "//" + name);
sw = fi.CreateText (); //直接重新寫入,如果要在原文件后面追加內容,應用fi.AppendText()
sw.WriteLine(info);
sw.Close();
sw.Dispose ();
}
讀取文件內容:
void LoadFileAndIntialSpeed (string sPath, string sName){
StreamReader sr = null;
sr = File.OpenText (sPath + "//" + sName);
string t_Line;
if ((t_sLine = sr.ReadLine ()) != null) {
//do some thing with t_sLine
} else {
print ("Null!");
}
sr.Close ();
sr.Dispose ();
}