如果路径已知,如何打开文件?

时间:2013-09-19 11:46:23

标签: c# listbox

我在listbox中有路径。我想在listbox项目点击打开文件。

我正在使用SelectedIndexChanged,从中我能够知道被点击的项目。现在,我想打开它。

1 个答案:

答案 0 :(得分:2)

public  void OpenDialogBox()
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "*YOURTEXTBOX.text";
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
               //Do something with Open File
            }
        }
        catch (Exception ex)
        {
           // You messed up
        }
    }
}