C# .txt 檔案讀寫

判斷檔案是否存在

if (!System.IO.File.Exists(filePath)){}

檔案寫入

using (FileStream fs = new FileStream(configPath, FileMode.Create, FileAccess.Write))
{
    using (StreamWriter sr = new StreamWriter(fs))
    {
        foreach (var item in dCurrentList)
            sr.WriteLine(item.Value.Path);
    }
}

檔案讀取

using (StreamReader sr = new StreamReader(configPath, System.Text.Encoding.UTF8))
{
    string line;
    while ((line = sr.ReadLine()) != null)
        configList.Add(line);
}

發佈留言