Opening text files in C# application
<p>I'm still a beginner when it comes to programming and this is a small application I did following a C# tutorial.    </p>

<pre><code>private void viewImagesToolStripMenuItem_Click(object sender, EventArgs e)
     {

string openedfile = "";

        openfd.Title = "Insert a text file";
        openfd.InitialDirectory = "C:";
        openfd.FileName = "";
        openfd.Filter = "text files|*.txt|word documents|*.doc|allfiles|*.*";


        if (openfd.ShowDialog() == DialogResult.Cancel)

        {
            MessageBox.Show("Operation canceled");
        }

        if (openfd.ShowDialog() != DialogResult.Cancel)

        {
            openedfile = openfd.FileName;
            richTextBox1.LoadFile(openedfile,RichTextBoxStreamType.PlainText);
        }
</code></pre>

<p>While doing this I noticed that if I change the same application's code order just 2 lines-  </p>

<pre><code>   string openedfile = "";
   openedfile = openfd.FileName; 
</code></pre>

<p>like below It will throw me an error like this when debugging - Empty path name is not legal. </p>

<pre><code>    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {


        openfd.Title = "Insert a text file";
        openfd.InitialDirectory = "C:";
        openfd.FileName = "";
        openfd.Filter = "text files|*.txt|word documents|*.doc|allfiles|*.*";

        **string openedfile = "";
        openedfile = openfd.FileName;**


        if (openfd.ShowDialog() == DialogResult.Cancel)

        {
            MessageBox.Show("Operation canceled");
        }

        if (openfd.ShowDialog() != DialogResult.Cancel)

        {

            richTextBox1.LoadFile(openedfile,RichTextBoxStreamType.PlainText);
        }
</code></pre>

<p>Isn't there a way to understand errors in these type situations. What is the specific order of coding an application like this?</p>
