获取我的SaveFileDialog文件夹的位置

时间:2014-03-11 08:46:46

标签: c# savefiledialog

嗨,我想在我的应用程序中创建备份数据 我使用了saveFileDialog,所以我可以将备份文件放在我想要的任何地方(Dekstop,驱动器D等)

mySaveFileDialog.FileName = "Backup Database " + dateTimeNow;
if (mySaveFileDialog.ShowDialog() == DialogResult.OK) 
{
    string fileAsal = System.IO.Path.Combine(Global.myDatabaseLocation, "data.mdb");
    System.IO.File.Copy(fileAsal, mySaveFileDialog.FileName, true);
}

问题是我想创建一个文件夹让我们说'myBackup'包含所有备份文件(db,image,movie) 这是我可以的

mySaveFileDialog.FileName = "Backup Database " + dateTimeNow;
if (mySaveFileDialog.ShowDialog() == DialogResult.OK)
{
    string pathDestination = "C:\\Users\\Maju\\Desktop\\";
    string nameFolder = "myBackup";
    string fileAsal = System.IO.Path.Combine(Global.myDatabaseLocation, "data.mdb");

    System.IO.Directory.CreateDirectory(@pathDestination + nameFolder);
    System.IO.File.Copy(fileAsal, mySaveFileDialog.FileName, true);
}

我无法得到用户想放置备份文件(pathDestination)的地方 任何想法怎么做?

2 个答案:

答案 0 :(得分:1)

只需使用FileDialog.InitialDirectory

 mySaveFileDialog.FileName = "Backup Database " + dateTimeNow;
 if (mySaveFileDialog.ShowDialog() == DialogResult.OK)
        {
            string pathDestination = mySaveFileDialog.InitialDirectory;

答案 1 :(得分:1)

mySaveFileDialog.FileName包含用户想要放置的备份文件路径。 你可以使用FileInfo来获取它。

FileInfo fi = new FileInfo(mySaveFileDialog.FileName);

\\Then you can use the properties of the FileInfo object to retrieve the
\\information you want:

fi.DirectoryName \\ the directory's full path