Save File to specific folder in c#, using SaveFileDialog

本秂侑毒 提交于 2019-12-13 05:43:29

问题


I need to save file using SaveFileDialog to specific folder..

For examaple, to save in "c:\MyNewFolder"

if the folder dosent exist so to create it and save, if the folder exist only save..

        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);
            }

        }

回答1:


You can look for

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



回答2:


string strPath="c:\MyNewFolder";

if (!Directory.Exists(strPath))

{

    Directory.CreateDirectory(strPath);

}

else

{

//Continue your logic and append your file name

}


来源:https://stackoverflow.com/questions/26944595/save-file-to-specific-folder-in-c-using-savefiledialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!