首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

openFileDialog 打开应用程序,该怎么解决

2012-01-19 
openFileDialog 打开应用程序private void button4_Click(object sender, EventArgs e){openFileDialog1.S

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);        }    }} 

热点排行