Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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#_Wpf_Textbox_Listbox_Streamreader - Fatal编程技术网

C# 将文件中的文本加载到文本框中

C# 将文件中的文本加载到文本框中,c#,wpf,textbox,listbox,streamreader,C#,Wpf,Textbox,Listbox,Streamreader,我正在尝试将文本文件加载到文本框中。要加载的文件是从列表框UnViewed_Messages中选择的,但是当我尝试加载该文件时,它不会执行任何操作 用于填充列表框的代码如下所示: public Quarantine_Messages() { InitializeComponent(); //loads in files to the list box on load DirectoryInfo DirFiles = new Director

我正在尝试将文本文件加载到
文本框中。要加载的文件是从列表框
UnViewed_Messages
中选择的,但是当我尝试加载该文件时,它不会执行任何操作

用于填充列表框的代码如下所示:

public Quarantine_Messages()
    {
        InitializeComponent();
        //loads in files to the list box on load
        DirectoryInfo DirFiles = new DirectoryInfo(@"C:\User\***\Documents\Noogle system\Quarantin_Messages");
        FileInfo[] Files = DirFiles.GetFiles("*.txt");
        foreach (FileInfo file in Files)
        {
            UnViewed_Messages.Items.Add(file.Name);
        }
    }
这是我用来尝试将文本文件加载到文本框
Message\u Viewer

 private void Open_Message_Content_Click(object sender, RoutedEventArgs e)
    {
        //tries to read in the file to the text box Message_Viewer
        string[] Files = Directory.GetFiles(@"C:\User\***\Documents\Noogle system\Quarantin_Messages");
        foreach (string file in Files)
        {
            if (System.IO.Path.GetFileName(file) != UnViewed_Messages.SelectedItems.ToString())
            {
                using (var viewer = new StreamReader(File.OpenRead(file)))
                {
                    Message_Viewer.Text = viewer.ReadToEnd();
                    viewer.Dispose();
                }
            }
        }
    }

如果您对此有任何帮助,我们将不胜感激,请提前感谢。

使用以下工具,而不是您的streamreader:

string[] lines = File.ReadAllLines(file);
Message_Viewer.Text = String.Join(Environment.NewLine, lines)
就这些。如果你知道这些东西,C#/.NET I/O真的很干净

编辑:请记住,对于非常大的文件,您仍然需要使用filestream逐行读取和添加,但这不应该是一个问题。我的意思是用一行行的文字填满你的内存,然后我们再谈,好吗


试试这样的方法,也许:

    private FileInfo[] files;
    private DirectoryInfo directory;

    private void Form1_Load(object sender, EventArgs e)
    {
        directory = new DirectoryInfo(@"C:\Users\smelendez\downloads");
        files = directory.GetFiles("*.txt");
        foreach (FileInfo file in files)
        {
            listBox1.Items.Add(file.Name);
        }
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedFile = files[listBox1.SelectedIndex];

        richTextBox1.Text = "";
        richTextBox1.Text = File.ReadAllText(selectedFile.FullName);
    }

然后根据您需要的逻辑从那里继续。

您遇到的错误/问题是什么?问题是当初始化
打开消息内容时,文件中的文本不会出现在文本框中,就像我希望的那样。您是否尝试过使用调试器查看消息查看器的文本设置?这可能会让您了解这是文件IO问题还是绑定问题。在foreach-loop.Run调试器的每一轮中,您都会覆盖
Message\u Viewer.Text
,以及为什么会发生此问题=如果txt文件很大,则会将内容加载到内存中。你的链接也是用德语写的。因为他是在给文本框添加文本,我想它不会那么大。否则,使用右边的ofcourse.ReadAllLines返回字符串数组。我用一个换行符将它们连接起来,这样它就可以显示在多行文本框中。readAllLines不会返回换行符。@Mafii再次-ReadAllText会返回换行符。你能给我一个解释或某种链接吗?这就是我要找的。我可以快速问一下吗?这似乎是一个愚蠢的问题,但为什么我不能使用
public quantial_Messages()
函数中声明的变量和字符串?如果我正确理解了这个问题,答案将是因为作用域。您超出了函数的范围,因此无法访问它们。您必须在函数外部定义一个变量(例如,为int指定val“123”)。然后在另一个函数中,可以使用该变量。