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

C# 流读取器无法将文本从文件文本获取到文本框

C# 流读取器无法将文本从文件文本获取到文本框,c#,filestream,C#,Filestream,我确信文件名是正确的 当我运行程序时,它会显示一个空文本框 结果 如其他地方所述,实际问题源于在执行对话框之前显示对话框阻塞 这里有几件事 为什么不创建一个专用表单,即MyDecisatedShowAllForm,而不是动态创建它呢 如果您使用的是实现IDisposable,最好使用using语句 示例 try { Form frmShow = new Form(); TextBox txtShowAll = new TextBox(); frmShow.StartPos

我确信文件名是正确的 当我运行程序时,它会显示一个空文本框

结果

如其他地方所述,实际问题源于在执行对话框之前显示对话框阻塞

这里有几件事

  • 为什么不创建一个专用表单,即MyDecisatedShowAllForm,而不是动态创建它呢

  • 如果您使用的是实现
    IDisposable
    ,最好使用
    using
    语句

  • 示例

    try
    {
        Form frmShow = new Form();
        TextBox txtShowAll = new TextBox();
        frmShow.StartPosition = FormStartPosition.CenterScreen;
        frmShow.Font = this.Font;
        frmShow.Size = this.Size;
        frmShow.Icon = this.Icon;
        frmShow.Text = "All data";
        txtShowAll.Dock = DockStyle.Fill;
        txtShowAll.Multiline = true;
        frmShow.Controls.Add(txtShowAll);
        frmShow.ShowDialog();
    
        StreamReader r = new StreamReader("empData.txt");
        string strShowAllData = r.ReadToEnd();                
        txtShowAll.Text = strShowAllData;
        r.Close();
    }
    catch (Exception x)
    {
         MessageBox.Show(x.Message);
    }
    
    using(var r = new StreamReader("empData.txt"))
    {
        string strShowAllData = r.ReadToEnd();                
        txtShowAll.Text = strShowAllData;
    }
    
    string strShowAllData = File.ReadAllText(path);
    
    // Set the Multiline property to true.
    textBox1.Multiline = true;
    // Add vertical scroll bars to the TextBox control.
    textBox1.ScrollBars = ScrollBars.Vertical;
    // Allow the RETURN key to be entered in the TextBox control.
    textBox1.AcceptsReturn = true;
    // Allow the TAB key to be entered in the TextBox control.
    textBox1.AcceptsTab = true;
    // Set WordWrap to true to allow text to wrap to the next line.
    textBox1.WordWrap = true;
    // Show all data
    textBox1.Text = strShowAllData;
    
  • 为什么不使用
    File.ReadAllText
    来代替,保存一些可打印的字符
  • 示例

    try
    {
        Form frmShow = new Form();
        TextBox txtShowAll = new TextBox();
        frmShow.StartPosition = FormStartPosition.CenterScreen;
        frmShow.Font = this.Font;
        frmShow.Size = this.Size;
        frmShow.Icon = this.Icon;
        frmShow.Text = "All data";
        txtShowAll.Dock = DockStyle.Fill;
        txtShowAll.Multiline = true;
        frmShow.Controls.Add(txtShowAll);
        frmShow.ShowDialog();
    
        StreamReader r = new StreamReader("empData.txt");
        string strShowAllData = r.ReadToEnd();                
        txtShowAll.Text = strShowAllData;
        r.Close();
    }
    catch (Exception x)
    {
         MessageBox.Show(x.Message);
    }
    
    using(var r = new StreamReader("empData.txt"))
    {
        string strShowAllData = r.ReadToEnd();                
        txtShowAll.Text = strShowAllData;
    }
    
    string strShowAllData = File.ReadAllText(path);
    
    // Set the Multiline property to true.
    textBox1.Multiline = true;
    // Add vertical scroll bars to the TextBox control.
    textBox1.ScrollBars = ScrollBars.Vertical;
    // Allow the RETURN key to be entered in the TextBox control.
    textBox1.AcceptsReturn = true;
    // Allow the TAB key to be entered in the TextBox control.
    textBox1.AcceptsTab = true;
    // Set WordWrap to true to allow text to wrap to the next line.
    textBox1.WordWrap = true;
    // Show all data
    textBox1.Text = strShowAllData;
    
  • 您可能需要将文本框设置为
  • 示例

    try
    {
        Form frmShow = new Form();
        TextBox txtShowAll = new TextBox();
        frmShow.StartPosition = FormStartPosition.CenterScreen;
        frmShow.Font = this.Font;
        frmShow.Size = this.Size;
        frmShow.Icon = this.Icon;
        frmShow.Text = "All data";
        txtShowAll.Dock = DockStyle.Fill;
        txtShowAll.Multiline = true;
        frmShow.Controls.Add(txtShowAll);
        frmShow.ShowDialog();
    
        StreamReader r = new StreamReader("empData.txt");
        string strShowAllData = r.ReadToEnd();                
        txtShowAll.Text = strShowAllData;
        r.Close();
    }
    catch (Exception x)
    {
         MessageBox.Show(x.Message);
    }
    
    using(var r = new StreamReader("empData.txt"))
    {
        string strShowAllData = r.ReadToEnd();                
        txtShowAll.Text = strShowAllData;
    }
    
    string strShowAllData = File.ReadAllText(path);
    
    // Set the Multiline property to true.
    textBox1.Multiline = true;
    // Add vertical scroll bars to the TextBox control.
    textBox1.ScrollBars = ScrollBars.Vertical;
    // Allow the RETURN key to be entered in the TextBox control.
    textBox1.AcceptsReturn = true;
    // Allow the TAB key to be entered in the TextBox control.
    textBox1.AcceptsTab = true;
    // Set WordWrap to true to allow text to wrap to the next line.
    textBox1.WordWrap = true;
    // Show all data
    textBox1.Text = strShowAllData;
    
  • 你为什么不先用计算机检查文件是否存在
  • Exmaple

    try
    {
        Form frmShow = new Form();
        TextBox txtShowAll = new TextBox();
        frmShow.StartPosition = FormStartPosition.CenterScreen;
        frmShow.Font = this.Font;
        frmShow.Size = this.Size;
        frmShow.Icon = this.Icon;
        frmShow.Text = "All data";
        txtShowAll.Dock = DockStyle.Fill;
        txtShowAll.Multiline = true;
        frmShow.Controls.Add(txtShowAll);
        frmShow.ShowDialog();
    
        StreamReader r = new StreamReader("empData.txt");
        string strShowAllData = r.ReadToEnd();                
        txtShowAll.Text = strShowAllData;
        r.Close();
    }
    catch (Exception x)
    {
         MessageBox.Show(x.Message);
    }
    
    using(var r = new StreamReader("empData.txt"))
    {
        string strShowAllData = r.ReadToEnd();                
        txtShowAll.Text = strShowAllData;
    }
    
    string strShowAllData = File.ReadAllText(path);
    
    // Set the Multiline property to true.
    textBox1.Multiline = true;
    // Add vertical scroll bars to the TextBox control.
    textBox1.ScrollBars = ScrollBars.Vertical;
    // Allow the RETURN key to be entered in the TextBox control.
    textBox1.AcceptsReturn = true;
    // Allow the TAB key to be entered in the TextBox control.
    textBox1.AcceptsTab = true;
    // Set WordWrap to true to allow text to wrap to the next line.
    textBox1.WordWrap = true;
    // Show all data
    textBox1.Text = strShowAllData;
    
  • 最后,您需要记住,您需要学习如何使用调试器和断点。我的意思是,如果您刚刚在
    txtShowAll.text=strShowAllData上设置了一个断点,那么您应该知道文本文件中是否有文本在您或我们的头脑中应该毫无疑问

  • 我刚刚注意到,在以对话框模式显示表单后,您正在向文本框添加文本。为什么不移动frmShow.ShowDialog();直到try块的末尾,就像我在下面的代码中所做的那样,并确保empData.txt存在于其路径中

    if(!File.Exists("someFile.txt"))
    {
        MessageBox.Show(!"oh nos!!!!! the file doesn't exist");
        return;
    }