Prevent an exception when I cancel an openfiledialog? : <p>I am having a problem when I click cancel on an openfiledialog in c#, it throws an exception. This is my code:</p>

<p>I tried to look at some other Stackoverflow solutions but none of them work. Here is a example:</p>

<pre><code>private void BtnCargarImagen_Click(object sender, EventArgs e)
        {
            //OFD = openfiledialog
            OFD.ShowDialog();
            PicFotografia.Image = Image.FromFile(OFD.FileName);
            MessageBox.Show("LA IMAGAN HA SIDO CARGADA");
            DialogResult result;
            if (result == DialogResult.OK)
            {
                return OFD.FileName;
            }

            return null; 
</code></pre>

<p>When the "if" part is placed on the code, it marks error on both "returns". </p>

<p>Without the dialog result part from top to bottom when I click on cancel, it marks an exception.</p>

<pre><code>private void BtnCargarImagen_Click(object sender, EventArgs e)
            {
                //OFD = openfiledialog
                OFD.ShowDialog();
                PicFotografia.Image = Image.FromFile(OFD.FileName);
                MessageBox.Show("LA IMAGAN HA SIDO CARGADA");
            }
</code></pre>

<p>Thanks in advance.</p>
