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

qvk1mo1f  于 2023-01-14  发布在  C#
关注(0)|答案(2)|浏览(160)

我需要使用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);
    }
}
chhkpiq4

chhkpiq41#

你可以找

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

ffscu2ro2#

string strPath="c:\MyNewFolder";

if (!Directory.Exists(strPath))

{

    Directory.CreateDirectory(strPath);

}

else

{

//Continue your logic and append your file name

}

相关问题