使用打开文件对话框选择文本文件的文件位置并在流中使用它 reader

Using open file dialog to choose the file location of a text file and use it in stream reader

我正在尝试创建一个 try catch 来检查 filelocation 中的文件是否存在。 如果它不希望打开文件对话框打开并允许用户能够选择文本文件(并且只有一个文本文件)并读取所选文件的文件位置并将其用作 sream reader

任何时候我使用这个文件位置都会一直到最后。而不是文件名,它将具有 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);

        }

添加这个

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

结果将是:

 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);

        }

我自己修复了它所以任何遇到类似问题的人都可以在这里找到解决方法

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;

        } 

这考虑到用户是否没有从文件对话框中select文件,或者是否select编辑了无效文件...

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);
                }