How to choose a folder without opening it with OpenFileDialog C# - 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.

Related

how to access OpenFileDialog with Streamwrite?

i want to make a windows forms app with 2 buttons
1 Button to find a .wtf or .txt file
Second Button to write a line into selected file let`s say "example"
this is my first button
private void button5_Click_1(object sender, EventArgs e)
{
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.*)|*.*";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = false;
if (choofdlog.ShowDialog() == DialogResult.OK)
{
string sFileName = choofdlog.FileName;
}
}
now how do i make the second button write "example" into the file i selected with the first one ?
You can achieve this in two steps:
First, you have to store the file name in a member variable:
private string _fileName;
private void button5_Click_1(object sender, EventArgs e)
{
OpenFileDialog choofdlog = new OpenFileDialog();
choofdlog.Filter = "All Files (*.*)|*.*";
choofdlog.FilterIndex = 1;
choofdlog.Multiselect = false;
if (choofdlog.ShowDialog() == DialogResult.OK)
{
_fileName = choofdlog.FileName;
}
}
In the second step, you have to define the logic of button 2:
private void secondButton_Click_1(object sender, EventArgs e)
{
System.IO.File.WriteAllText( _fileName, "Example" );
}
"1 Button to find a .wtf or .txt file"
To look for specific file extensions you should change your filter a bit
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "txt files (*.txt)|*.txt|wtf files (*.wtf)|*.wtf";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
Microsoft docs

Openfiledialog problem with File Description Error

Iam using VS2015 - Windows Forms. When I click my Browse Button the OpenFileDialog Works good. But Suppose once I Re-click the button after to refresh the form data's, the OpenFileDialog simply hang-up.
I can't understand my problem.. Any of the superiors can guide me?
MyFileNameStr = String.Empty;
openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "(*.xlsx)|*.xls| All files (*.*)|*.*";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Title = "Select Your Attachment File :- ";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFileDialog1.FileName.Length>0) {
String MyDrawingFile = Path.GetFileName(openFileDialog1.FileName);
myDataGrid1.CurrentRow.Cells["MyExcel_file"].Value = Path.GetFileName(openFileDialog1.FileName);
MyFileNameStr = openFileDialog1.SafeFileName.ToString();
MyFileNameStrs = openFileDialog1.SafeFileName.ToString().Split('_');
}
Thanks Again
I have added the few more below codes for file descriptions, then it works good.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Title = "Select Your Attachment File :- ";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.Filter = "exe files | *.exe|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
MyDrawingFile = System.IO.Path.GetFileName(openFileDialog1.FileName).ToString();
MyFileNameStr = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName).ToString();
}
Thanks
this is due to
if you click on button and browse a file then the process is running on your excel file.
if you again click on button the process is busy with your excel file and app will get hang.

How do a make this type of select folder dialog in C#?

So I recently tried to the FolderBrowserDialog but much to my disappointment it was not like the following screenshot:
But instead it was formatted and as I think, hard to navigate with like this:
How would I go about getting the other version where it's a dialog box asking for what folder to save to like the select file type natively, instead of what I think is this hard to navigate menu.
The CommonOpenFileDialog class from the NuGet Package "Microsoft.WindowsAPICodePack-Shell" will answer your request.
Set IsFolderPicker property to true and that's it.
using Microsoft.WindowsAPICodePack.Dialogs;
private bool SelectFolder(out string fileName)
{
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
fileName = dialog.FileName;
return true;
}
else
{
fileName = "";
return false;
}
}
thats because you are using FolderBrowserDialog instead of OpenFileDialog
you can check the below
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "Browse File";
fileDialog.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fileDialog.FilterIndex = 2;
fileDialog.InitialDirectory = "c:\\";
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = fileDialog.FileName;
}
}

Initial directory for OpenFileDialog

The file dialog has to open the last directory location that was used before it was shut down, but I have no idea how to do this. My colleague only shows me the example of word, when you click "file" it shows the last used files, he told me to use a register or an INI file, which I have never used before.
Here is the code I am using:
string f_sOudeLocatie = #"D:\path\is\classified";
private void btBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Zoek de CSV file";
fdlg.InitialDirectory = f_sOudeLocatie;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
tbGekozenBestand.Text = fdlg.FileName;
tbVeranderNaamIn.Text = Path.GetDirectoryName(fdlg.FileName);
f_sOudeLocatie = Path.GetDirectoryName(fdlg.FileName);
f_sSourceFileName = fdlg.FileName;
f_sDestFileName = Path.GetFileName(Path.GetDirectoryName(fdlg.FileName)) + ".csv";
btOpslaan.Enabled = true;
tbVeranderNaamIn.ReadOnly = false;
}
}
if you'll create the OpenFileDialog outside the button click event it should remember the last folder you've been
string f_sOudeLocatie = #"D:\path\is\classified";
OpenFileDialog fdlg = new OpenFileDialog();
public Form1()
{
InitializeComponent();
fdlg.Title = "Zoek de CSV file";
fdlg.InitialDirectory = f_sOudeLocatie;
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
}
private void btBrowse_Click(object sender, EventArgs e)
{
if (fdlg.ShowDialog() == DialogResult.OK)
{
fdlg.InitialDirectory = fdlg.FileName.Remove(fdlg.FileName.LastIndexOf("\\"));// THIS LINE IS IMPORTENT
tbGekozenBestand.Text = fdlg.FileName;
tbVeranderNaamIn.Text = Path.GetDirectoryName(fdlg.FileName);
f_sOudeLocatie = Path.GetDirectoryName(fdlg.FileName);
f_sSourceFileName = fdlg.FileName;
f_sDestFileName = Path.GetFileName( Path.GetDirectoryName(fdlg.FileName) ) + ".csv";
btOpslaan.Enabled = true;
tbVeranderNaamIn.ReadOnly = false;
}
}
You need to set
fdlg.RestoreDirectory = false;
Reason:
RestoreDirectory property makes sure that the value in
Environment.CurrentDirectory will be reset before the OpenFileDialog
closes. If RestoreDirectory is set to false, then
Environment.CurrentDirectory will be set to whatever directory the
OpenFileDialog was last open to. As explained here
You can use registry to store the last directory location. And each time you open the file dialogue, get the value from the registry and set as the default location. When it closed store the location back to registry.
This code project article explains you well about reading and writing to registry
ReadWriteDeleteFromRegistry
If you choose to use INI file, some search will give you examples of how to read and write from INI file

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