简体   繁体   中英

Using SaveFileDialog in c# Winforms

I'm basically just trying to get a file path to save a file to but my SaveFileObject won't let me access the SelectedPath. I've checked the other forums and can't figure out why it won' tlet me, here's my code;

SaveFileDialog filePath = new SaveFileDialog();
 DialogResult result = filePath.ShowDialog();


     if (result == DialogResult.OK)
     {
         string folderPath = filePath.;
     }

It'll let me select filePath.ShowDialog again and filePath.ToString etc... Where am I going wrong?

You actually want the file name from the FileName property from your SaveFileDialog. This will give you the full path and file name for the file your user wants to save.

SaveFileDialog saveDialog = new SaveFileDialog();
DialogResult result = saveDialog.ShowDialog();
if (result == DialogResult.OK)
{
    String fileName = saveDialog.FileName;
    //your code to save the file;
}

Although, since .ShowDialog() returns a DialogResult, you can use it directly in the if to spare one line of code (man I'm greedy)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM