使用SaveFileDialog将文件保存到c#中的特定文件夹

时间:2014-11-15 09:53:10

标签: c# winforms savefiledialog

我需要使用SaveFileDialog将文件保存到特定文件夹..

例如,要保存在“c:\ MyNewFolder”

如果文件夹存在,则创建它并保存,如果该文件夹仅存在保存..

        String fileName="";
        String date = DateTime.Now.Day+"-"+DateTime.Now.Month+"-"+DateTime.Now.Year;
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.FileName = fileName;
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            using (Stream s = File.Open(saveFileDialog1.FileName,FileMode.CreateNew))

            using (StreamWriter sw = new StreamWriter(s))
            {
                sw.WriteLine(tbName.Text);
                sw.WriteLine(tbSummary.Text);
            }

        }

2 个答案:

答案 0 :(得分:1)

你可以寻找

SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "c:\\MyNewFolder";
save.RestoreDirectory = true;

答案 1 :(得分:1)

string strPath="c:\MyNewFolder";

if (!Directory.Exists(strPath))

{

    Directory.CreateDirectory(strPath);

}

else

{

//Continue your logic and append your file name

}