How do I show a Save As dialog in WPF?

后端 未结 6 2117
鱼传尺愫
鱼传尺愫 2020-12-13 08:46

I have a requirement in WPF/C# to click on a button, gather some data and then put it in a text file that the user can download to their machine. I can get the first half of

6条回答
  •  清歌不尽
    2020-12-13 09:19

    Both answers thus far link to the Silverlight SaveFileDialogclass; the WPF variant is quite a bit different and differing namespace.

    Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
    dlg.FileName = "Document"; // Default file name
    dlg.DefaultExt = ".text"; // Default file extension
    dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
    
    // Show save file dialog box
    Nullable result = dlg.ShowDialog();
    
    // Process save file dialog box results
    if (result == true)
    {
        // Save document
        string filename = dlg.FileName;
    }
    

提交回复
热议问题