Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用“打开文件”对话框选择文本文件的文件位置并在流读取器中使用它_C#_Openfiledialog - Fatal编程技术网

C# 使用“打开文件”对话框选择文本文件的文件位置并在流读取器中使用它

C# 使用“打开文件”对话框选择文本文件的文件位置并在流读取器中使用它,c#,openfiledialog,C#,Openfiledialog,我正在尝试做一个try-catch,它将检查filelocation中的文件是否存在。 如果没有,我希望打开“打开文件”对话框,允许用户选择文本文件(且仅为文本文件),读取所选文件的文件位置,并将其用作sream阅读器的文件位置 任何时候我使用这个文件的位置将正确,直到结束。它将使用bin/debug/ok代替文件名 try { if (!File.Exists(filelocation)) { t

我正在尝试做一个try-catch,它将检查filelocation中的文件是否存在。 如果没有,我希望打开“打开文件”对话框,允许用户选择文本文件(且仅为文本文件),读取所选文件的文件位置,并将其用作sream阅读器的文件位置

任何时候我使用这个文件的位置将正确,直到结束。它将使用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;

        } 

这将考虑用户是否未从“文件”对话框中选择文件,或者是否选择了无效文件

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

您不需要抛出
FileNotFound
异常,相反,您可以在IF语句中包含逻辑,如所示。查看异常用法。虽然此代码片段可以解决此问题,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。@Xaruth不是。他有
DialogResult=OFD.ShowDialog()这使它等于确定哪一个剂量起作用。它是
String result=OFD.FileName当我在我的问题答案中添加时,他复制并添加了过滤器。仍然没有达到我想要的
DialogResult=OFD.ShowDialog()然后
字符串结果=OFD.FileName
使其与文件位置相等。