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();
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
At the moment I have three buttons on a form, each opens a different form (form2 with a textbox to display the text from the textfile, form3 with a picturebox to display the image)
What I am trying to do is put the two together for my last button so the user can filter which type to open (TXT Files or Image files). I am not sure how I can put the two together and get them to work.
The code I used to just open text files:
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = #"C:\";
ofd.Filter = "TXT Files(*.txt;)|*.txt;";
if(ofd.ShowDialog() == DialogResult.OK)
{
using(StreamReader rdText = new StreamReader(ofd.FileName))
{
string info = File.ReadAllText(ofd.FileName);
TextDocumentForm newTextDocument = new TextDocumentForm();
newTextDocument.TextFileName = info;
newTextDocument.Show();
}
}
}
What I use to open my image files
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofdi = new OpenFileDialog();
ofdi.InitialDirectory = #"C:\";
ofdi.Filter = "Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;.bmp;";
if (ofdi.ShowDialog() == DialogResult.OK)
{
Image image = Image.FromFile(ofdi.FileName);
ImgDoc newImageDoc = new ImgDocumentForm();
newImageDoc.ImageShow = image;
newImageDoc.Show();
}
}
Any help is appreciated as I am trying to develop my understanding of how OpenFileDialog still works.
Combining filters:
var openFile = new OpenFileDialog
{
InitialDirectory = #"C:\",
Filter = "TXT Files(*.txt;)|*.txt;|Image Files(*.jpg;*.jpeg;*.bmp)|*.jpg;*.jpeg;.bmp;"
};
Then use Path.GetExtension() to see which route you should take:
if (openFile.ShowDialog() == true)
{
var ext = System.IO.Path.GetExtension(openFile.FileName);
if (ext == ".txt")
{
// Open text file
}
else if (ext == ".jpg" || ext == ".jpeg" || ext == ".bmp")
{
// Open image file
}
}
void OpenWithDialog()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Triangle polygon file|*.poly";
if (ofd.ShowDialog() == DialogResult.OK)
{
OpenPolyFile(ofd.FileName);
}
}
void OpenPolyFile(string file)
{
var geometry = TriangleNet.IO.FileReader.ReadPolyFile(file);
// ...
}
private void button1_Click(object sender, EventArgs e)
{
}
How to read files in button1 click directly?
what you want is that your geometry object be accessible in additional scopes besides OpenPolyFile(). So you can simply make the geometry declaration accessible to both methods, declaring it, say, in the form code behind
// class scoped variables
[ThePolyFileType] (pick the right one here :) geometry = null;
void OpenWithDialog()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Triangle polygon file|*.poly";
if (ofd.ShowDialog() == DialogResult.OK)
{
OpenPolyFile(ofd.FileName);
}
}
void OpenPolyFile(string file)
{
geometry = TriangleNet.IO.FileReader.ReadPolyFile(file);
// ...
}
private void button1_Click(object sender, EventArgs e)
{
if (geometry != null)
{
//do your stuff
}
}
How do i read already opened file in a another button click event
directly .(i.e without open file dialogue in button click)
I'm not quite sure why you want to read it twice, but if that requirement says, make selected filename available globally and use it.
private string filename;
void OpenWithDialog()
{
var ofd = new OpenFileDialog();
ofd.Filter = "Triangle polygon file|*.poly";
if (ofd.ShowDialog() == DialogResult.OK)
{
OpenPolyFile(ofd.FileName);
filename = ofd.FileName
}
}
Now you have opened filename available in button_clcik you can use this file and read again.
private void button1_Click(object sender, EventArgs e)
{
// now you can read the file.
//File.ReadAllText(filename); //OR
TriangleNet.IO.FileReader.ReadPolyFile(file);
}
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);
}
}
}
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;
}
}
}
}