我做了一个记事本,用的是textbox控件,读取文本文件可以正常打开,可是读取一些非文本文件(如exe文件)只能显示前几个字符,而系统带的记事本可以完全打开,虽然全是乱码。我试过用RichTextBox的loadfile也能完整打开,但把内容复制到textbox控件里面又只有前面的两三个字符了,所以请高手指教。我不想使用RichTextBox,况且系统自带的记事本也是用textbox控件写成的。我参阅过http://www.autoitx.com/viewthread.php?tid=11154&page=1,上面说可能因为读取的数据中有NULL字符而显示不全,解决方式是把NULL字符替换为其他字符。可是他是用vb语言,看了半天还是不懂。请高手帮我点一下,让我的记事本可以像系统带的那样读取非文本文件中的字符串。
我之后加上Encoding.Default.GetString(rawBytes);
打开文件还是原先那样。
int i = BitConverter.ToInt32(rawBytes, 0);
string s = Encoding.Default.GetString(rawBytes, 4, 60); //ASCII encoding
int i = BitConverter.ToInt32(rawBytes, 0);
这句话貌似没用啊,我加上开始打开exe文件还是只有前几个字符,可以加qq,我发截图帮我看下吗?
我qq244267863
然后显示时:
ToString("X").PadLeft(2, '0')
{
long len;
int ptr=0;
System.Text.Encoding txtEncode=null;
byte[] bytes;
using (FileStream fs = new FileStream(@"d:\new.jpg", FileMode.Open))
{
len=fs.Length;
bytes = new byte[len];
fs.Read(bytes, 0, bytes.Length);
using (StreamReader sr = new StreamReader(fs))
{
txtEncode=sr.CurrentEncoding;
}
} textBox1.Text = txtEncode.GetString(bytes);
MessageBox.Show(textBox1.TextLength.ToString()); if (txtEncode == System.Text.Encoding.Unicode)
{
do
{
ptr = Array.IndexOf(bytes, (byte)0, ptr);
if (ptr > -1)
{
if (ptr >= bytes.Length - 1)
{
break;
} if (bytes[ptr + 1] == 0)
{
bytes[ptr] = 32;
bytes[ptr + 1] = 32;
} ptr += 2; }
}
while (ptr > -1);
}
else
{
do
{
ptr = Array.IndexOf(bytes, (byte)0, ptr);
if (ptr > -1)
{
bytes[ptr] = 32;
ptr += 1;
}
}
while (ptr > -1);
} textBox1.Text = txtEncode.GetString(bytes);
MessageBox.Show(textBox1.TextLength.ToString());
}将NULL字符替换为空格
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
fpath = openFileDialog1.FileName; byte[] rawBytes = System.IO.File.ReadAllBytes(fpath);
int j = 0;
foreach (byte b in rawBytes)
{
uint u = Convert.ToUInt32(b);
if (u == 0)
{
rawBytes[j] = Convert.ToByte(u + 32);
++j;
}
else
{
++j;
}
}
text.Text = Encoding.Default.GetString(rawBytes);
}
经过调试可以把内容全部显示出来了,可速度出奇的慢啊,我和系统带的做对比,打开一个不到1M的文件,基本要慢3倍的时间,有没有效率高一点的办法。
for (int i = 0; i < rawBytes.Length; i++)
{
if (rawBytes[i] == 0)
{
rawBytes[i] = 32;
}
}
text.Text = Encoding.Default.GetString(rawBytes);
{
public Form1()
{
InitializeComponent();
}
/// <summary>
///江新飞
/// 有些功能暂时还未能实现,还望见谅。
/// </summary>
/// <param name="sender">Copy Right for JiangXinfei</param>
/// <param name="e">If you have any problem,sende an E_mail to me </param>
/// <![CDATA[2008-10-12 ]]>
private void Form1_Load(object sender, EventArgs e)
{
ContextMenu MyMenu = new ContextMenu();
//MyMenu.MenuItems.Add(this.MenuItem6.Clone());
//MyMenu.MenuItems.Add(this.MenuItem11.Clone());
//MyMenu.MenuItems.Add(this.MenuItem7.Clone());
//MyMenu.MenuItems.Add(this.MenuItem12.Clone());
//MyMenu.MenuItems.Add(this.MenuItem8.Clone());
this.richTextBox1.ContextMenu = MyMenu;
} private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
openFileDialog1.Filter = "*.txt|*.txt";
string MyFileName = this.openFileDialog1.FileName;
if (MyFileName.Trim() == "")
return;
StreamReader MyReader = null;
try
{
MyReader = new StreamReader(MyFileName, System.Text.Encoding.Default);
this.richTextBox1.Text = MyReader.ReadToEnd();
}
catch//(Exception )
{
MessageBox.Show("读取文本发生错误,请检查原文件是否是文本文件!","信息提示",MessageBoxButtons .OK ,MessageBoxIcon.Information );
return;
}
finally
{
if (MyReader != null)
{
MyReader.Close();
}
}
} private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.saveFileDialog1.ShowDialog();
saveFileDialog1.Filter = "*.txt|*.txt";
string MyFileName = this.saveFileDialog1.FileName;
if (MyFileName.Trim() == "") return;
StreamWriter MyWriter = null;
try
{
MyWriter = new StreamWriter(MyFileName, false, System.Text.Encoding.Default);
MyWriter.Write(this.richTextBox1.Text);
}
catch (Exception )
{
MessageBox .Show ("写文本时发生错误!","信息提示",MessageBoxButtons .OK ,MessageBoxIcon.Information );
return;
}
finally
{
if (MyWriter != null)
{
MyWriter.Close();
}
}
} private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
} private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
} private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1 .Cut ();
} private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Copy();
} private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Paste();
} private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Undo();
} private void 重做ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.Redo();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (richTextBox1.Text == "")
{
Application .Exit ();
}
else
{
MessageBox.Show("窗口即将关闭,是否保存?", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.saveFileDialog1.ShowDialog();
saveFileDialog1.Filter = "*.txt|*.txt";
string MyFileName = this.saveFileDialog1.FileName;
if (MyFileName.Trim() == "") return;
StreamWriter MyWriter = null;
try
{
MyWriter = new StreamWriter(MyFileName, false, System.Text.Encoding.Default);
MyWriter.Write(this.richTextBox1.Text);
}
catch (Exception)
{
MessageBox.Show("写文本时发生错误!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
finally
{
if (MyWriter != null)
{
MyWriter.Close();
}
}
}
} private void 打开OToolStripButton_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
openFileDialog1.Filter = "*.txt|*.txt";
string MyFileName = this.openFileDialog1.FileName;
if (MyFileName.Trim() == "")
return;
StreamReader MyReader = null;
try
{
MyReader = new StreamReader(MyFileName, System.Text.Encoding.Default);
this.richTextBox1.Text = MyReader.ReadToEnd();
}
catch//(Exception )
{
MessageBox.Show("读取文本发生错误,请检查原文件是否是文本文件!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
} finally
{
if (MyReader != null)
{
MyReader.Close();
}
}
} private void 保存SToolStripButton_Click(object sender, EventArgs e)
{
this.saveFileDialog1.ShowDialog();
string MyFileName = this.saveFileDialog1.FileName;
saveFileDialog1.Filter = "*.txt|*.txt";
if (MyFileName.Trim() == "") return;
StreamWriter MyWriter = null;
try
{
MyWriter = new StreamWriter(MyFileName, false, System.Text.Encoding.Default);
MyWriter.Write(this.richTextBox1.Text);
}
catch (Exception)
{
MessageBox.Show("写文本时发生错误!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
finally
{
if (MyWriter != null)
{
MyWriter.Close();
}
}
} private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
//string []s=new string [1048576];
//for (string q = richTextBox1.SelectedText; ; q++)
//{
//}
} private void 新建NToolStripButton_Click(object sender, EventArgs e)
{
} private void 剪切UToolStripButton_Click(object sender, EventArgs e)
{
this.richTextBox1.Cut();
} private void 复制CToolStripButton_Click(object sender, EventArgs e)
{
this.richTextBox1.Copy ();
} private void 粘贴PToolStripButton_Click(object sender, EventArgs e)
{
this.richTextBox1.Paste ();
}
}
}
//仅 对文本 进行 读写操作file:///E:\Asert\scan\up.txt
StreamReader sr = new StreamReader(fs);
//定位操作点,begin 是一个参考点
sr.BaseStream.Seek(0, SeekOrigin.Begin);
//读一下,看看文件内有没有内容,为下一步循环 提供判断依据
//sr.ReadLine() 这里是 StreamReader的方法 可不是 console 中的~