0

I am new to WPF, I checked around for solutions to my question but I haven't quite found the right way to do things. I am making an application where the user can make an account and add a profile image. So in the Create_Account_Page I have a button that on click has an event to create a OpenFileDialog and then call dialog.showdialog();

What I want is to be able to get the selected file and save it to my project folder. I know that there is a FileOk event and it seems its what I need but all the solutions I've found don't work.

J-Alex
  • 6,881
  • 10
  • 46
  • 64
Alvaromon
  • 190
  • 2
  • 16
  • @PeterDuniho The question posed is a duplicate but it ended up being a reference issue. where my project needed to add a ref to system.windows.form. To any one reading this, theres an OpenFileDialog in System.Windows.Forms and one in system.Win32 dont mix the two! – Alvaromon Jun 17 '17 at 23:54

1 Answers1

3
var dialog = new Microsoft.Win32.OpenFileDialog();
var newDestination = Environment.CurrentDirectory;

if (dialog.ShowDialog() == true)
{
    var fullPath = dialog.FileName;
    var fileOnlyName = Path.GetFileName(fullPath);
    File.Copy(fullPath, Path.Combine(newDestination, fileOnlyName));
}
dovid
  • 6,354
  • 3
  • 33
  • 73
  • `if (dialog.ShowDialog())` should be all you need – Blue Jun 17 '17 at 23:09
  • 1
    @FrankerZ `ShowDialog` return Nullable (bool?). so cant. – dovid Jun 17 '17 at 23:11
  • what i have seen online everyone is doing dialog.showdialog() == DialogResult.OK but that isnt working for me – Alvaromon Jun 17 '17 at 23:16
  • @Alvaromon this in case `System.Windows.Forms.OpenFileDialog`. my code demostration `Microsoft.Win32.OpenFileDialog` but besides, everything is the same – dovid Jun 17 '17 at 23:24
  • @lomed thank you this helped me im new to WPF so i didnt realize that my project didnt have a reference to System.Windows.Forms – Alvaromon Jun 17 '17 at 23:53