open file dialog in c# (2 methods)
you created an app that needs to open an external file manually, for example you created a player in c# and you want to let the user open up files that's why you use openfiledialog which prompt a dialog file and let you choose an specific file.
to do here is the code only for openfiledialog
i will provide you two methods 1.for text files 2. windows media player
please add using.sytem.IO namespace first
1.method for text :
//allow you to open a dilogbox and load the file you desire
OpenFileDialog record = new OpenFileDialog();
record.FileName = "";
record.Filter = "Text Files|*.txt";
if (record.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string file = record.FileName;
string[] text = File.ReadAllLines(file);
listBox2.Items.AddRange(text);
}
2.method for windows media player :
using (FileDialog dlgopenfile = new OpenFileDialog())
{
dlgopenfile.Filter = "all files|*.*";
dlgopenfile.FileName = "";
if (dlgopenfile.ShowDialog() == System.Windows.Forms.DialogResult.OK)// show dilog baraie in ke betavan panjarei barai entekhabe media enekhb kard in tabe be esme modal function ham niz shenakhte mishavad //
{
try
{
mediaplayer.URL = dlgopenfile.FileName;
label1.Text = dlgopenfile.FileName;
}
catch (Exception ex)
{
MessageBox.Show("unable to load the file " + ex.Message);
}
}
}
Comments
Post a Comment