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