Open File Dialog and Select a File Using Wpf Controls and C#

Open file dialog and select a file using WPF controls and C#

Something like that should be what you need

private void button1_Click(object sender, RoutedEventArgs e)
{
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

// Set filter for file extension and default file extension
dlg.DefaultExt = ".png";
dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";

// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();

// Get the selected file name and display in a TextBox
if (result == true)
{
// Open document
string filename = dlg.FileName;
textBox1.Text = filename;
}
}

Select File and Continue Program In WPF

Previously I had the same problem with WPF. When you are working with WPF, System.Windows.Form Namespace is not included to your Project references;

and in the other hand actually, there are two OpenFileDialog, the first is System.Windows.Forms.OpenFileDialog (this is what you have) and the second is Microsoft.Win32.OpenFileDialog. if you want to get your code to work you must add System.Windows.Forms into your references:

Solution Explorer -> YourProject -> References (Right Click and Add Reference...) -> Assembly -> Framework -> Find And Select System.Windows.Forms -> OK

and the Next Solution is to use Microsoft.Win32, it's pretty easy. just add this Namespace into your code file and Change your code like this:

 string file = "";
// Displays an OpenFileDialog so the user can select a file.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Files|*.txt;*.out";
openFileDialog1.Title = "Select a File";

// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog1.ShowDialog() == true)
{
file = openFileDialog1.FileName;
//file = openFileDialog1.OpenFile().ToString();
//openFileDialog1.Dispose();
}
openFileDialog1 = null;
Console.WriteLine("File path is: " + file);

WPF how to get selected file from OpenFileDIalog

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));
}

Open file dialog and select a file using WPF controls and Ironpython

You can use the default OpenFileDialog in the .net framework. Just do the following things.

You need to import it:

from Microsoft.Win32 import OpenFileDialog

Than use it in your click event:

dialog = OpenFileDialog()
dialog.Filter = "All Files|*.*"
if dialog.ShowDialog():
selectedFile = dialog.FileName

The variable selectedFile will contain the path you want. Hope this helps.

WPF choosing an image via file dialog and interpolating it to a certain size

Create a BitmapImage and set its DecodePixelWidth or DecodePixelHeight property:

if (openFileDialog.ShowDialog() == true)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(openFileDialog.FileName);
bitmap.DecodePixelHeight = 200;
bitmap.EndInit();

ImageBox.Source = bitmap;
}


Related Topics



Leave a reply



Submit