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!
Related
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
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.
so what I'm trying to do is load a .txt file and once the .txt file is loaded it will show the contents from the .txt file in a listView.
Here is my load code.
List<String> proxies = new List<string>();
private void loadProxiesToolStripMenuItem_Click(object sender, EventArgs e)
{
loadProxies();
}
private void loadProxies()
{
this.Invoke(new MethodInvoker(delegate
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "TXT files|*.txt";
ofd.Title = "Load Proxies";
var dialogResult = ofd.ShowDialog();
if (dialogResult == DialogResult.OK)
{
proxies = new List<string>();
Parallel.ForEach(System.IO.File.ReadLines(ofd.FileName), (line, _, lineNumber) =>
{
if (line.Contains(":"))
{
//loadedCombo.Add(line);
proxies.Add(line);
}
else
{
//MessageBox.Show("Hmm, thats not a combolist - please try again");
}
});
}
txt_proxies.Text = "Proxies Loaded: " + proxies.Count.ToString();
}));
}
and I'm wanting it to show in the listView which is named "proxyView".
So what I'm trying to say it, I can get the .txt to load and it changes the count but it's not adding the contents from the .txt file into the listview.
Thanks much appreciated.
To add an item to a ListView you can use yourListView.Items.Add(text)
For example:
private void loadProxies()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "TXT files|*.txt";
ofd.Title = "Load Proxies";
var dialogResult = ofd.ShowDialog();
if (dialogResult == DialogResult.OK)
{
foreach (var line in System.IO.File.ReadLines(ofd.FileName))
{
if (line.Contains(":"))
proxyView.Items.Add(line);
}
}
}
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;
}
}
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