日期:2014-05-18  浏览次数:20763 次

openFileDialog 打开应用程序
private void button4_Click(object sender, EventArgs e)
  {
  openFileDialog1.ShowDialog();
  openFileDialog1.OpenFile();
  }

是这样写吗,但是选择一个.exe .doc .xls文件就是没有反应

------解决方案--------------------
当然没反映了,你打开后要干嘛都没写啊.
------解决方案--------------------
Process.Start(openFileDialog1.Filename);
------解决方案--------------------
C# code
  OpenFileDialog open = new OpenFileDialog();
                        if (open.ShowDialog() == DialogResult.OK)
                        {
                            System.Diagnostics.Process.Start(open.FileName);
                        }

------解决方案--------------------
設置所要打開的文件格式..openFileDialog1 屬性里有
------解决方案--------------------
发重了.
------解决方案--------------------
如果openFileDialog1.ShowDialog() 的返回值是DialogResult.OK
那你可以用openFileDialog1.Filename做你想做的事情啊!

------解决方案--------------------
private void button4_Click(object sender, EventArgs e) 

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{


/////你自己想做的事


}
 
}
------解决方案--------------------
探讨
嗯 ,但是 OpenFile 有什么用啊,为什么它就不打开?

------解决方案--------------------
OpenFile确实可以打开文件,不过是以 Stream 的形式打开的,除非你可以自己来解释这个流,否则不会像双击一样打开这个文件。
------解决方案--------------------
举例:
OpenFile 方法用于提供一种从对话框快速打开文件的功能。出于安全目的,以只读方式打开文件。若要以读/写模式打开文件,必须使用其他方法,如 FileStream。

C# code

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    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)
            {
                using (myStream)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
}