-2

I am trying to make a try catch that will check if the file in filelocation exists or not. If it doesnt i want the open file dialog to open and allow user to be able choose the text file (and only a text file) and read the file location of the selected file and use that as the file location sting for the sream reader

any time i use this the file location will be right until the end. instead of the file name it will have bin/debug/ok

 try
        {
            if (!File.Exists(filelocation))
            {
                throw new FileNotFoundException();
            }
            else
            {
                StreamReader question = new StreamReader(filelocation);
            }
        }
        catch (System.IO.FileNotFoundException)
        {
            MessageBox.Show("File containing the questions not found");
            OpenFileDialog OFD = new OpenFileDialog();
            DialogResult result = OFD.ShowDialog();
            string filelocation = result.ToString();
            StreamReader question = new StreamReader(filelocation);

        }
JhWOLFJh
  • 67
  • 2
  • 15
  • You don't need to throw a `FileNotFound` exception, instead you could have the logic in your IF statement, as [Abdul Rehman Sayed](http://stackoverflow.com/a/28857033/3191303) demonstrates. Check out [how-expensive-are-exceptions-in-c](http://stackoverflow.com/questions/891217/how-expensive-are-exceptions-in-c) for exception usage. – AWinkle Mar 04 '15 at 14:35

3 Answers3

2

add this

OFD .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

the result will be:

 try
        {
            if (!File.Exists(filelocation))
            {
                throw new FileNotFoundException();
            }
            else
            {
                StreamReader question = new StreamReader(filelocation);
            }
        }
        catch (System.IO.FileNotFoundException)
        {
            MessageBox.Show("File containing the questions not found");
            OpenFileDialog OFD = new OpenFileDialog();
            OFD .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            DialogResult result = OFD.ShowDialog();
            string filelocation = result.ToString();
            StreamReader question = new StreamReader(filelocation);

        }
Armen Mkrtchyan
  • 921
  • 1
  • 13
  • 34
  • `DialogResult = OFD.ShowDialog();` then `String result = OFD.FileName;`to make it equal to the file location. – JhWOLFJh Mar 13 '15 at 19:27
1

This takes into account whether User did not select a File from the File Dialog or whether invalid file is selected...

StreamReader ReadME;
                if (!File.Exists(termfilelocation))
                {
                    MessageBox.Show("File containing the questions not found");                    
                    OpenFileDialog OFD = new OpenFileDialog();

                    OFD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                    // this will filter out any file that isnt a text file
                    ofd.CheckFileExists = true;//this will not allow invalid files.
                    DialogResult dr = OFD.ShowDialog();

                    if (dr == DialogResult.Cancel)
                    {
                        //User did not select a file.
                        return;
                    }
                    String result = OFD.FileName;

                    ReadME = new StreamReader(result);
                    termfilelocation = result;
                }
                else
                {
                    ReadME = new StreamReader(termfilelocation);
                }
Abdul Rehman Sayed
  • 6,532
  • 7
  • 45
  • 74
-3

I fixed it myslef so anybody having similar problems heres how to fix it

try
        {
            if (!File.Exists(termfilelocation))
            {
                throw new FileNotFoundException();
            }
            else
            {
                StreamReader reader = new StreamReader(termfilelocation);
            }
        }
        catch (System.IO.FileNotFoundException)
        {
            MessageBox.Show("File containing the questions not found");
            // this will display a message box sying whats in ("")
            OpenFileDialog OFD = new OpenFileDialog();   
            // this will create a new open file dialog box
            OFD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            // this will filter out any file that isnt a text file
            DialogResult = OFD.ShowDialog();

            String result = OFD.FileName;
             // this will give the result to be the file name 
             // that was choosen in the open file dialog box 
            StreamReader reader = new StreamReader(result);
            termfilelocation = result;

        } 
JhWOLFJh
  • 67
  • 2
  • 15
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – gunr2171 Mar 04 '15 at 16:13
  • @Xaruth it isnt. He had `DialogResult result = OFD.ShowDialog();` which makes it equal ok which dosent work. it is `String result = OFD.FileName;` as i added in my answer to my question which he copied and added the filter. Still didnt work to achieve what i was looking for – JhWOLFJh Mar 13 '15 at 19:22