winform中,读取txt文件,并显示在textbox中,遇到2个问题,
1无法显示汉字
2不能完全显示,只能显示部分,我设了多行显示,把textBox2.MaxLength = 99999也不行,也有滚动条,
是不是textBox有默认的最大限制,那我用什么显示比较好?listbox我试了也不行,richtextbox也不行
下面是代码
string path;
path= Path.GetDirectoryName(Application.ExecutablePath) + "\\"+ dataGridView1.CurrentRow.Cells[4].Value .ToString ();
string stype = path.Substring(path.LastIndexOf("."));//上面是路径,没有问题
if (stype == ".txt")
{
if (!File.Exists(path))
{
MessageBox.Show("文件不存在或已经删除");
}
else
{
FileStream fs = File.OpenRead(path);
byte[] arr = new byte[1000];
UTF8Encoding data = new UTF8Encoding(true); while (fs.Read(arr, 0, arr.Length) > 0)
{
textBox2.Text = data.GetString(arr);
}
}
}
else
{
MessageBox.Show("只能阅读.txt文件,其他文件请下载");
}
1无法显示汉字
2不能完全显示,只能显示部分,我设了多行显示,把textBox2.MaxLength = 99999也不行,也有滚动条,
是不是textBox有默认的最大限制,那我用什么显示比较好?listbox我试了也不行,richtextbox也不行
下面是代码
string path;
path= Path.GetDirectoryName(Application.ExecutablePath) + "\\"+ dataGridView1.CurrentRow.Cells[4].Value .ToString ();
string stype = path.Substring(path.LastIndexOf("."));//上面是路径,没有问题
if (stype == ".txt")
{
if (!File.Exists(path))
{
MessageBox.Show("文件不存在或已经删除");
}
else
{
FileStream fs = File.OpenRead(path);
byte[] arr = new byte[1000];
UTF8Encoding data = new UTF8Encoding(true); while (fs.Read(arr, 0, arr.Length) > 0)
{
textBox2.Text = data.GetString(arr);
}
}
}
else
{
MessageBox.Show("只能阅读.txt文件,其他文件请下载");
}
解决方案 »
- C#怎样在Mapx上面添加动态轨迹
- 图片链接会直接打开
- 某程序根据输入的总分和课程数目计算平均分。写出实现下面输入输出形式的输入和输出语句。
- 做欢迎动画的flash 什么软件比较好
- 如何判断用户的手机支持WAP1.1还是WAP2.0?
- 在线等,在VS。NET里,WINDOWS应用程序开发,DATAGRID数据源之后,怎么绑定段,
- 我的自定义控件没有显示在工具栏中,如何让它显示出来?
- ValueMember 和 DisplayMember
- 請問,我用什麼namespace可以列舉局域網所有的電腦或用戶?謝謝!
- XML反序列化问题,高手帮忙
- 如何打开文本文档里面的内容(在线等)
- 如何用visual studio 2005 设计类图?
path= Path.GetDirectoryName(Application.ExecutablePath) + "\\"+ dataGridView1.CurrentRow.Cells[4].Value .ToString ();
string stype = path.Substring(path.LastIndexOf("."));//上面是路径,没有问题
if (stype == ".txt")
{
if (!File.Exists(path))
{
MessageBox.Show("文件不存在或已经删除");
}
else
{
FileStream fs = File.OpenRead(path);
StreamReader sr = new StreamReader(fs,Encoding.Default);
textBox2.Text = sr.ReadToEnd();
}
}
else
{
MessageBox.Show("只能阅读.txt文件,其他文件请下载");
}
如果是UTF-8 采用1000的byte是不行的 UTF-8汉字长度是3另外textBox2.MaxLength = 99999不知道是什么意思,
写软件的看到99999 应该想到要么报错,要么长度不为最大另外读数据改成
byte[] arr = new byte[ 4096 ];
int read = fs.Read( arr, 0, arr.Length );
while ( read > 0 )
{
textBox2.Text = data.GetString( arr,0,read );
read = fs.Read( arr, 0, arr.Length );
}string stype = path.Substring(path.LastIndexOf("."));
改成System.IO.Path.GetExtension( path ).ToLower();
byte[] arr = new byte[1000];
UTF8Encoding data = new UTF8Encoding(true); while (fs.Read(arr, 0, arr.Length) > 0)
{
textBox2.Text = data.GetString(arr);
}
---------》
using(StreamReader sr = new StreamReader(path,Encoding.Default))//这里的Encoding是你文本的编码
{
textBox2.Text = sr.ReadToEnd();
}
byte[] arr = new byte[ 4096 ];
int read = fs.Read( arr, 0, arr.Length );
while ( read > 0 )
{
textBox2.Text = data.GetString( arr,0,read );
read = fs.Read( arr,…
[/Quote] textBox2.Text = data.GetString( arr,0,read );
这个data是哪来的啊?