how to access OpenFileDialog with Streamwrite? - c#

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

Related

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!

Choose file C# and get directory

I'm trying to open a file dialog box so the user can choose the location of an access database. Can someone explain how to add a file dialog when a button is clicked and also how to transform the user choice into a string that contains the file directory ( c:\abc\dfg\1234.txt)?
Thanks
As you did not state the technology you use (WPF or WinForms), I assume you use WinForms. In that case, use an OpenFileDialog in your code. After the dialog was closed, you can get the selected full file name using the FileName property.
There is the following example of how to use it on the documentation page I linked above, which I modified slightly as you want the file name, not the stream:
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "Database files (*.mdb, *.accdb)|*.mdb;*.accdb" ;
openFileDialog1.FilterIndex = 0;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
string selectedFileName = openFileDialog1.FileName;
//...
}
}
Based on your previous question I assume you are using WinForms.
You can use the OpenFileDialog Class for this purpose. See the code below which will run on your button Click event assuming your button id is button1:
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "Access files (*.accdb)|*.accdb|Old Access files (*.mdb)|*.mdb";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
var path = openFileDialog1.FileName;
}
}
More information.
Assuming that you actually have a form with button (button1)...
At the constructor hook into button1's click event
...
button1.Click += button1_Click;
...
Then define the handling function and use System.Windows.Forms.OpenFileDialog as you like.
void button1_Click(object sender, EventArgs e)
{
string oSelectedFile = "";
System.Windows.Forms.OpenFileDialog oDlg = new System.Windows.Forms.OpenFileDialog();
if (System.Windows.Forms.DialogResult.OK == oDlg.ShowDialog())
{
oSelectedFile = oDlg.FileName;
// Do whatever you want with oSelectedFile
}
}
It's actually fairly simple
namespace YourProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string path = "";
//Declare the File Dialog
OpenFileDialog ofd = new OpenFileDialog();
private void button1_click(object sender, EventArgs e)
{
if (odf.ShowDialog() == DialogResult.OK)
{
path = ofd.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

How do you use an specific file from a C# multiselection dialog?

I need to use an specific file from the ones you select from a file dialog.
OpenFileDialog ofd = new OpenFileDialog();
private void pictureBox23_Click(object sender, EventArgs e)
{
ofd.Filter = "WAV|*.wav";
this.ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
label23.Text = ofd.SafeFileName;
}
else
{
label23.Text = "No files selected...";
}
}
What i need to do is select and use the files I pre-define, so if I define an event with 01.wav if the user selects a file named 01.wav, that one will be used like so:
using (SoundPlayer player = new SoundPlayer(Beatpadpc.Properties.Resources._01))
{
player.Play();
}
I currently have it adapted as it will play it from the resources, what i need to do is to play the file from the file selecion, but only if the file is named "01".wav
Is there a way for doing it?
Well, just filter the ofd property called Filenames.
OpenFileDialog ofd = new OpenFileDialog();
string strAudioFilePath = String.Empty;
private void pictureBox23_Click(object sender, EventArgs e)
{
ofd.Filter = "WAV|*.wav";
this.ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK)
{
// Here you make a small filter with linq
label23.Text = strAudioFilePath = ofd.FileNames.FirstOrDefault(x => x.EndsWith("01.wav")); // you change 01.wav to something that make more sense to you
}
else
{
label23.Text = "No files selected...";
}
}
Then if you want to get rid of your resource file, just give your sound player the path of the file and that's all.
SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath);
simpleSound.Play();

How do I show the filename in listbox but keep the relative path using openfiledialog?

I am making a program in which the user needs to load in multiple files. However, in the ListBox I need to show only file names of the files they loaded but still be able to use the files loaded. So I want to hide the full path. This is how I load a file into the ListBox now, but it shows the whole path:
private void browseBttn_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;
OpenFileDialog1.Filter = "DLL Files|*.dll";
OpenFileDialog1.Title = "Select a Dll File";
if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
dllList.Items.AddRange(OpenFileDialog1.FileNames);
}
}
// Set a global variable to hold all the selected files result
List<String> fullFileName;
// Browse button handler
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
OpenFileDialog1.Multiselect = true;
OpenFileDialog1.Filter = "DLL Files|*.dll";
OpenFileDialog1.Title = "Seclect a Dll File";
if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// put the selected result in the global variable
fullFileName = new List<String>(OpenFileDialog1.FileNames);
// add just the names to the listbox
foreach (string fileName in fullFileName)
{
dllList.Items.Add(fileName.Substring(fileName.LastIndexOf(#"\")+1));
}
}
}
// handle the selected change if you wish and get the full path from the selectedIndex.
private void dllList_SelectedIndexChanged(object sender, EventArgs e)
{
// check to make sure there is a selected item
if (dllList.SelectedIndex > -1)
{
string fullPath = fullFileName[dllList.SelectedIndex];
// remove the item from the list
fullFileName.RemoveAt(dllList.SelectedIndex);
dllList.Items.Remove(dllList.SelectedItem);
}
}
You can extract the fileName of the absolute path using the static Class Path in the System.IO namespace
//returns only the filename of an absolute path.
Path.GetFileName("FilePath");

Categories