none
OpenFolder Dialog + CSharp + VS 2010

    Question

  • I am trying to write an application for Windows Vista and W7.  Intially I started with WPF and then did not find the OpenFolder or SelectFolder dialog, so I decided to switch to WinForms.  Then there is advice and warnings about how Winforms is going away and WPF is the future.

    I have restarted my project and searched for OpenFolder dialog which is a key data input variable.  Is there anyway to use some other namespaces to address this WPF deficiency?

    Any advice and help is appreciated.

    Thank you in advance.

    Monday, July 25, 2011 2:28 PM

Answers

  • Hello,

    you can use the openfile dialog in code behind like this:

       /// <summary>
        /// Избира снимка, която да бъде прикачена към продукта!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowseProduct_Click(object sender, RoutedEventArgs e)
        {
          Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
          openFileDialog.InitialDirectory = "c:\\";
          openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
          openFileDialog.FilterIndex = 2;
          openFileDialog.RestoreDirectory = true;
          Nullable<bool> result = openFileDialog.ShowDialog();
          if (result == true)
          {
            textPicProduct.Text = openFileDialog.FileName;
            ImgAddProduct.Source = new BitmapImage(new Uri(openFileDialog.FileName, UriKind.Absolute));
            FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
            PicProduct = new byte[fs.Length];
            fs.Read(PicProduct, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
          }
        }
    

     the code above opens file dialog and shows only certain formats. Here you can implement the logic you need. There is no drag and drop like in windows forms. Here is a little link with tutorial: http://rpelepei.blogspot.com/2010/01/using-open-file-dialog-window-in-wpf.html. Good luck.

     

    Monday, July 25, 2011 2:48 PM

All replies

  • Hello,

    you can use the openfile dialog in code behind like this:

       /// <summary>
        /// Избира снимка, която да бъде прикачена към продукта!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowseProduct_Click(object sender, RoutedEventArgs e)
        {
          Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
          openFileDialog.InitialDirectory = "c:\\";
          openFileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|Jpeg files (*.jpg)|*.jpg|All valid files (*.bmp/*.jpg)|*.bmp/*.jpg";
          openFileDialog.FilterIndex = 2;
          openFileDialog.RestoreDirectory = true;
          Nullable<bool> result = openFileDialog.ShowDialog();
          if (result == true)
          {
            textPicProduct.Text = openFileDialog.FileName;
            ImgAddProduct.Source = new BitmapImage(new Uri(openFileDialog.FileName, UriKind.Absolute));
            FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read);
            PicProduct = new byte[fs.Length];
            fs.Read(PicProduct, 0, System.Convert.ToInt32(fs.Length));
            fs.Close();
          }
        }
    

     the code above opens file dialog and shows only certain formats. Here you can implement the logic you need. There is no drag and drop like in windows forms. Here is a little link with tutorial: http://rpelepei.blogspot.com/2010/01/using-open-file-dialog-window-in-wpf.html. Good luck.

     

    Monday, July 25, 2011 2:48 PM
  • Thank you for the solution and the link!

    Monday, July 25, 2011 5:00 PM