Forums - C# Corner

Forum guidelines
Sangram Mohanty

Sangram Mohanty

  • 1.5k
  • 1
  • 58

unable to create/save a file using SaveFileDialog in WPF

Sep 13 2017 7:13 AM
I am designing a WPF application that is able to create a new .txt file in a desired location, edit it by inserting contents in a textblock, and then saving it. However I am unable to create a new file and unable to save it in a desired location.

I am using commands like SaveFileDialog, Filestream as below: -

private SaveFileDialog newFileDialog = null;\\Inside public partial class MainWindow : Window
newFileDialog = new SaveFileDialog();\\ Inside public MainWindow() to create an object
newFileDialog.FileOk += newFileDialogFileOk;\\ public MainWindow() to call the function

saveFileDialog.Filter = "All items | *.*";\\ Inside function private void newFile(object sender, RoutedEventArgs e)... These are event driven functions
saveFileDialog.ShowDialog();\\ Inside function private void newFile(object sender, RoutedEventArgs e)... These are event driven functions

/*Inside function private void newFileDialogFileOk*/
string filefullpath = saveFileDialog.FileName;
FileInfo sr = new FileInfo(filefullpath);
if (sr.Exists)
\{
sr.Replace(filefullpath, filefullpath);
}
else
\{
using (FileStream reader = sr.Create())
\{
Byte[] txt = new UTF8Encoding(true).GetBytes("");
reader.Write(txt, 0, txt.Length);

Attachment: FileMenu.zip

Answers (1)