C# – WPF copy file to the predefined directory

cfilewpf

In a WPF app I need to make it possible for a user to pick a file by the standard Open File Dialog and save it to the predefined folder (user doesn't know where it is) right after the user click OK button on Open File Dialog. Something like of importing a file to the application. I do it in the following way:

        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();            
        dlg.Filter = "Text documents (.txt)|*.txt"; 
        Nullable<bool> result = dlg.ShowDialog();
        if (result == true)
        {
            string filename = dlg.SafeFileName;
            System.IO.File.Copy(filename, @"E:\TestFolder\" + filename);
            MessageBox.Show("File " + filename + " saved");
        }

Is there a standard way to check if the file already exists before trying to save it and if it is really saved after saving it?

Best Answer

Look at System.File.Exists that should be able to tell you what you need to know.