Hi,

was wondering if it is possible to use openfiledialog to find a file and then use streamreader to read the text and use it in a variable? Got this so far but it comes up with this error: "Object reference not set to an instance of an object." the error appears on the "lblhighscore.Text = SR.ReadLine();" line

 OpenFileDialog HiScoreOpen = new OpenFileDialog();
                        HiScoreOpen.Filter = "TXT|*.txt";
                        if (HiScoreOpen.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {

                            string openFileLocation = HiScoreOpen.FileName;


                            StreamReader SR = null;

                            SR =  new StreamReader(HiScoreOpen.FileName);

                            lblhighscore.Text = SR.ReadLine();
                            SR.Close();
                            highscore = lblhighscore.text;

Thanks

I will suggest to do the following to read the line:

if (sr.Peek() > -1) 
   {
       lblhighscore.Text = sr.ReadLine();
   }

This will test if there is some thing to read before launhing the read line function.

Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.