-2

代码:

namespace bla_bla_bla

{   public delegate void pathSelected(string path);
 //...
  public partial class Form1 : Form
  {
      public pathSelected onPath;
 //...
  private void button1_Click(object sender, EventArgs e)
  {

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter ="Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
    openFileDialog1.Multiselect = true;
    openFileDialog1.ShowDialog();
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    if (openFileDialog1.SafeFileName != null)
    onPath(openFileDialog1.SafeFileName);
  }

问题:

为什么我null在最后一行得到参考例外?

错误:

(原始海报应在此处添加)

4

1 回答 1

0

不完全理解您的问题,或者为什么需要委托,但在您的 Button1 单击事件处理程序上尝试这样的事情:

int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); 
if (result == DialogResult.OK) {
    string file = openFileDialog1.FileName;
    try {
        string text = File.ReadAllText(file);
        size = text.Length;
    }
    catch (IOException) {
    }
}
于 2013-05-19T19:08:00.217 回答