Is there a way to set filenames from OpenFileDialog through code? - c#

Is there a way to set filenames from OpenFileDialog through code?
string[] files = {"D:\\test1.txt","D:\\test2.txt"};
System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
dialog.Multiselect = true;
dialog.FileNames = files;
Error while trying to set the FileNames:
Error CS0200 Property or indexer 'FileDialog.FileNames' cannot be assigned to -- it is read only

Actually, you can use InitialDirectory to set the default address
openFileDialog.InitialDirectory = "D:\\";
and use openFileDialog.Filter to set specific type
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
and you can use openFileDialog1.FileName to select a file
openFileDialog1.FileName = "test1"
or muliplefile
openFileDialog1.FileName = "\"test1\" \"test2\"";
For example:
string message = "";
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt";
openFileDialog1.FileName = "\"test1\"\"test2\"";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (string file in openFileDialog1.FileNames)
{
message += Path.GetFileName(file) + " - " + file + Environment.NewLine;
}
MessageBox.Show(message);
}

Related

Use OpenFileDialog Read FileName But read ""

In my WpfAPP I use a button to select file to read the fileName the code is
private void Select_File(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog()
{
Filter = "|*.csv;*.xls;*.xlsx",
Title = "Read File",
Multiselect = false
};
bool? res = ofd.ShowDialog();
if (res != null && res.Value)
{
Debug.WriteLine("==========================");
string strOpenFile = ofd.FileName;
Debug.WriteLine(strOpenFile);
}
}
but the fileName is ""
How to read the fileName successfully?
your filter syntax is wrong. Try this
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "CSV files (*.csv)|*.csv|Excel (*.xls)|*.xls";
ofd.Title = "Read File";
ofd.Multiselect = false;
bool? res = ofd.ShowDialog();
if (res != null && res.Value)
{
Debug.WriteLine("==========================");
string strOpenFile = ofd.FileName;
Debug.WriteLine(strOpenFile);
}

DataGrid add function to cell button

Hello all I wanted to ask how I can add function to button in dataGridCell, so when click button it open file explorer, I select file and it return file path in other cell.
The function I want add under button of datagrid cell "Browse":
public string test5()
{
var fileContent = string.Empty;
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\users\\Desktop";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
}
return filePath;
}
}
then I wrote under button load data with this button function I want use above:
private void button1_Click(object sender, EventArgs e)
{
//xDxD();
//xDxD2();
//textBox1.Text = xDxD2();
//this.dataGridView1.Rows.Add("five", "six", "seven", "eight");
this.dataGridView1.Rows.Insert(0, "one", "two", "three", test5());
}
But when I click button1, then it auto start function from this cell.
Sorry for my english.
You were very close to succeeding
If this code is not helpful you should check this out: Extracting Path from OpenFileDialog path/filename
But here is my code:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\users\\Desktop";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
var filePath = Path.GetDirectoryName(openFileDialog.FileName);
textBox1.Text = filePath;
}
Hope you found what you need!

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.

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

strFileName in open file dialog

I am fairly new to c#.
My question is what is strFileName in open file dialog?
I have this code currently:
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
It brings up an error on strFileName. i cant find an explanation as to what it does in this code.
Any help will be appreciated and i apologise if this question has been asked before.
Without knowing what the error is, just by looking at your code, you are probably getting a compile error on strFileName because it is not declared:
You can either change your code to this:
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
input = open.FileName;
if (input == String.Empty)
return;
Or this:
string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
It doesn't give me an error - though I did have to declare it. Did you?
string strFileName = ""; // added this line - it compiles and runs ok
private void TestFunc()
{
string input = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
strFileName = open.FileName;
if (strFileName == String.Empty)
return;
}
you need declare strFileName first
string strFileName = string.empty();
then use it.
You need to declare the variable strFilename as a string:
string strFileName = string.Empty;
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.* ";
open.InitialDirectory = "C:";
if (open.ShowDialog() == DialogResult.OK)
{
strFileName = open.FileName;
}
/* you probably don't want this unless it's part of a larger block of code
if (strFileName == String.Empty)
{
return;
}
*/
return strFileName;

Categories