Skip to main content
 首页 » 编程设计

c#之如何使用 FileOpenDialog 在 C# 中打开文件

2024年02月05日17arxive

我试图通过按下一个按钮(更确切地说是一个标签,但它的工作方式相同)来打开一个文件

出于某种原因,当 FileDialog 打开并且我选择文件并按下打开时,它不会打开文件,它只会关闭 FileDialog

private void selectLbl_Click(object sender, EventArgs e) 
{ 
 
    OpenFileDialog ofd = new OpenFileDialog(); 
    ofd.InitialDirectory = "c:\\"; 
    ofd.Filter = "Script files (*.au3)|*.au3"; 
    ofd.RestoreDirectory = true; 
    ofd.Title = ("Select Your Helping Script"); 
 
 
    if (ofd.ShowDialog() == DialogResult.OK) 
    { 
        ofd.OpenFile(); //Not sure if there is supposed to be more here 
    } 
} 

请您参考如下方法:

ofd.OpenFile(); 

将文件内容作为字节流返回,如所述 here .如果你想像你描述的那样打开文件,使用

if (ofd.ShowDialog() == DialogResult.OK) 
{ 
  System.Diagnostics.Process.Start(ofd.FileName); 
} 

因此您选择的文件以关联的应用程序开头。