Open a File through Open File Dialog in any Event using C#

T.saravanan
Posted by T.saravanan under Windows Forms category on | Points: 40 | Views : 4006
Hi All,

Here i am given a code snippet for to Open a file in any event with based on that file format.

// Here i am explain in Button Click Event

Private void button_Click(object sender, EventArgs e)
{
OpenFileDialog opObject=new OpenFileDialog();
opObject.Filter="All files (*.*)| *.* ";
if(opObject.ShowDialog() ==DialogResult.OK)
{
System.Diagnostics.Process processObj = new System.Diagnostics.Process();
processObj.StartInfo.UseShellExecute = true;
processObj.StartInfo.FileName = opObject.FileName; \\ Ex: C:\\Test\\Sample.xls
processObj.Start();
}
}


If you have a file name in text file means (For Example in Text File have C:\Test\Sample.xls)

Private void button_Click(object sender, EventArgs e)
{
OpenFileDialog opObject=new OpenFileDialog();
opObject.Filter="(*.txt)| *.txt ";
if(opObject.ShowDialog() ==DialogResult.OK)
{
string sFilePath = System.IO.File.ReadAllText(opObject.FileName); \\ Its working only single line in Text File else use Split function in String
System.Diagnostics.Process processObj = new System.Diagnostics.Process();
processObj.StartInfo.UseShellExecute = true;
processObj.StartInfo.FileName = sFilePath;
processObj.Start();
}

}

Cheers :)

Comments or Responses

Login to post response