C# - File

目录的判断和创建

if (!Directory.Exists(sPath))
{
     Directory.CreateDirectory(sPath);
}

 

文件的打开

private void Button_TemplateInputer_Click(object sender, EventArgs e)
{
    using(OpenFileDialog olg = new OpenFileDialog())
    {
        if(olg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string path = olg.FileName;
        }
    }
}

 

文件读取

private void FileRead()
{
    using (StreamReader sr = new StreamReader(path, Encoding.Default))
    {
        template = sr.ReadToEnd();
    }
}

 

文件写入

void FileWriter()
{
    FileStream fs = new FileStream(filePath, FileMode.Create);
    StreamWriter sw = new StreamWriter(fs);

    try
    {
        sw.Write(content);
        sw.Flush();
    }
    finally
    {
        sw.Close();
        fs.Close();
    }
}

 

posted @ 2015-07-27 11:44  `Laimic  阅读(127)  评论(0)    收藏  举报