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

I have a TextBox named textbox1 and a Button named button1.
When I click on button1 I want to browse my files to search only for image files (type jpg, png, bmp...).
And when I select an image file and click Ok in the file dialog I want the file directory to be written in the textbox1.text like this:
textbox1.Text = "C:\myfolder\myimage.jpg"

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

var ofd = new Microsoft.Win32.OpenFileDialog() {Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"};
var result = ofd.ShowDialog();
if (result == false) return;
textBox1.Text = ofd.FileName;

Related

How to choose a folder without opening it with OpenFileDialog C#

I have to create a button with which the user has to choose a folder.
I've try with OpenFileDialog, But there I can not select the folder and the folder just open.
This is my code:
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "C# Corner Open File Dialog";
fdlg.InitialDirectory = #"D:\dosfiles\ValPoch";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
label2.Text = fdlg.FileName;
label2.Show();
}
I try with this code, He works perfectly, But I not like the window, who opened, Is too small.
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
dlg.Description = "Select a folder";
dlg.SelectedPath = #"D:\dosfiles\ValPoch\";
if (dlg.ShowDialog() == DialogResult.OK)
{
label2.Text = dlg.SelectedPath;
label2.ForeColor = Color.Red;
label2.Show();
}
}
How can I fix my code with OpenFileDIalog to select a folder not to open that folder ?
Thank you.
Take a look at Ookii.Dialogs, a great library in order to use common Windows dialogs including file dialogs.

How do I create a copy of a picture to a specific path in winform

I have 2 system a c# winform and a php and their database is stored in a single database mysql now my problem is storing pictures...., in my html I save my pictures in htdocs/"foldername"/productimages now what I want in my C# winform was get the location path of the picture from open dialogue and copy that picture to the specific folder which is the htdocs/"foldername"/productimages how do i do that
my code
string picloc;
private void UpdBtn_Click(object sender, EventArgs e)
{
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|ALL Files(*.*)|*.*";
dlg.Title = "Select Thumbnail";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Result();
picloc = dlg.FileName.ToString();
pic1.ImageLocation = picloc;
}
}
so How do I copy the file picture from the string picloc to the specific loc?
You can use File.Copy to do this. Something like the following should manage the copy for you:
string picloc;
string new_loc;
private void UpdBtn_Click(object sender, EventArgs e)
{
dlg.Filter = "JPG Files(*.jpg)|*.jpg|PNG Files(*.png)|*.png|ALL Files(*.*)|*.*";
dlg.Title = "Select Thumbnail";
if (dlg.ShowDialog() == DialogResult.OK)
{
// Result();
picloc = dlg.FileName.ToString();
pic1.ImageLocation = picloc;
File.Copy(picloc, new_loc); // new_loc being the new location for the file.
}
}
This assumes you already know the location for the copy of the file. If you want to give the user the choice, do so via a SaveFileDialog to get the new location (as a string) and then perform the copy.

OpenFileDialog only select file path

I am trying to use a OpenFileDialog to let the user select a local file that can interact with my app. The file can be in use when the user selects it as I really only want to get the file's path. My issue is that if I try doing this with a OpenFileDialog, when the user presses "open", it actually tries to open the file even though I don't call for opening anywhere. Since the file is in use, it throws an error.
Is there any way for me to present a dialog for a user to just select a file an do nothing else?
Heres my current code:
DialogResult result = this.qbFilePicker.ShowDialog();
if (result == DialogResult.OK)
{
string fileName = this.qbFilePicker.FileName;
Console.WriteLine(fileName);
}
Make sure you are using
using System.Windows.Forms;
Then the following code from MSDN will not open your file unless you explicitly uncomment the bit that opens the file :)
(Take out the bits that don't pertain to you such as the .txt filter etc)
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
Console.WriteLine(fileName);
//to open the file
/*
try
{
Stream myStream = null;
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
* */
}
}

Open image location from textbox

I have openfiledialog that reading user image address with file info and load it in textbox
I want to have another button in order to open image address (that already saved in textbox)
how to code this button at wpf ? I know i should use process.start but no idea !
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
tbl_Moshtari tt = new tbl_Moshtari();
dlg.FileName = "pic-file-name"; // Default file name
dlg.DefaultExt = ".jpg"; // Default file extension
dlg.Filter = "JPEG(.jpeg)|*.jpeg | PNG(.png)|*.png | JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif"; // Filter files by extension
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
//// picbox.Source = new BitmapImage(new Uri(dlg.FileName, UriKind.Absolute));
//bitmapImage = new BitmapImage();
//bitmapImage.BeginInit();
//bitmapImage.StreamSource = System.IO.File.OpenRead(dlg.FileName);
//bitmapImage.EndInit();
////now, the Position of the StreamSource is not in the begin of the stream.
//picbox.Source = bitmapImage;
FileInfo fi = new FileInfo(dlg.FileName);
string filename = dlg.FileName;
txt_picaddress.Text = filename;
System.Windows.MessageBox.Show("Successfully done");
}
This second button i have
private void btn_go_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
//FileInfo fi = new FileInfo(dlg.FileName);
string filename = dlg.FileName;
Process.Start(filename);
}
This isnt working for me .
Process.Start() should open up the image as long as filename is an absolute path to the file. With that being said, where in your btn_go_Click method are you actually opening up the dialog to get the file name? dlg.FileName returns an empty string if you don't show the dialog in which case Process.Start() fails.
If the file name needs to come from the previous dialog, you shouldn't create a new dialog; instead, change
Process.Start(filename)
to
Process.Start(txt_picaddress.Text)
Of course, you need to do some input verification to make sure the path is correct (unless the textbox is read-only).
Also, consider setting a breakpoint on string filename = dlg.FileName; to make sure it has the correct path to the file if it's still not working.
To open and highlight the file in Windows Explorer:
string filename = txt_picaddress.Text;
ProcessStartInfo pInfo =
new ProcessStartInfo("explorer.exe", string.Format("/Select, {0}", filename));
Process.Start(pInfo);
In your second code sample, you created a new instance of openFileDialog, you need instead to use the previous instance of the openFileDialog that is holding the correct image filename:
if you create the first openFileDialog in the window constructor you can do this:
private void btn_go_Click(object sender, RoutedEventArgs e)
{
string filename = this.dlg.FileName;
Process.Start(filename);
}
hope this helps, this is what i can say given the code you provided.
You don't need an OpenFileDialog in btn_go_Click if you want to use the path in your textbox:
private void btn_go_Click(object sender, RoutedEventArgs e)
{
string filename = txt_picaddress.Text;
Process.Start(filename);
}

Obtain file path of C# save dialog box

I've got a save dialog box which pops up when i press a button. However i dont want to save a file at that point, i want to take the name and place it in the text box next to the button, for the name to be used later.
Can anybody tell me how to obtain the file path from the save dialog box to use it later?
Here is a sample code I just wrote very fast... instead of Console.Write you can simply store the path in a variable and use it later.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Filter = "Your extension here (*.EXT)|*.ext|All Files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Console.WriteLine(saveFileDialog1.FileName);//Do what you want here
}
Addressing the textbox...
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = saveFileDialog.FileName;
}
private void mnuFileSave_Click(object sender, EventArgs e)
{
dlgFileSave.Filter = "RTF Files|*.rtf|"+"Text files (*.txt)|*.txt|All files (*.*)|*.*";
dlgFileSave.FilterIndex = 1;
if (dlgFileSave.ShowDialog() == System.Windows.Forms.DialogResult.OK && dlgFileSave.FileName.Length > 0)
{
foreach (string strFile in dlgFileSave.FileNames)
{
SingleDocument document = new SingleDocument();
document.rtbNotice.SaveFile(strFile, RichTextBoxStreamType.RichText);
document.MdiParent = this;
document.Show();
}
}
}
Try below code.
saveFileDialog1.ShowDialog();
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);

Categories