高手看看这段代码如何支持中文显示.
程序作用主要是自动监测C:\MSSQLTips下是否有文本文件,如果发现有自动读取里面内容并且显示出来,现在的问题是,只能读取英文或者数字,如果是中文,会显示??????,请教大家如何修改。
// MessageBox.Show(e.Name);
try
{
using (StreamReader sr = new StreamReader(@"C:\MSSQLTips\" + e.Name))
{
String line = sr.ReadToEnd();
sr.Dispose();
sr.Close(); string line2 = line.Replace("\r\n", "\u0000");
Packet packet = new Packet(0x7025);
packet.WriteUInt8((byte)7);
packet.WriteUInt8((byte)0);
packet.WriteAscii(line2);
if (!string.IsNullOrEmpty(line2))
{
Agent.Send(packet);
}
System.IO.File.Delete(@"C:\MSSQLTips\" + e.Name);
}
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
} private void button9_Click_1(object sender, EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\MSSQLTips\"; watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;
watcher.Filter = "*.txt"; //watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged); watcher.EnableRaisingEvents = true;
watcher.InternalBufferSize = 64000;
label31.Text = "自动公告启动";
}
程序作用主要是自动监测C:\MSSQLTips下是否有文本文件,如果发现有自动读取里面内容并且显示出来,现在的问题是,只能读取英文或者数字,如果是中文,会显示??????,请教大家如何修改。
// MessageBox.Show(e.Name);
try
{
using (StreamReader sr = new StreamReader(@"C:\MSSQLTips\" + e.Name))
{
String line = sr.ReadToEnd();
sr.Dispose();
sr.Close(); string line2 = line.Replace("\r\n", "\u0000");
Packet packet = new Packet(0x7025);
packet.WriteUInt8((byte)7);
packet.WriteUInt8((byte)0);
packet.WriteAscii(line2);
if (!string.IsNullOrEmpty(line2))
{
Agent.Send(packet);
}
System.IO.File.Delete(@"C:\MSSQLTips\" + e.Name);
}
}
catch (Exception ex)
{
MessageBox.Show("error" + ex);
}
} private void button9_Click_1(object sender, EventArgs e)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\MSSQLTips\"; watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size;
watcher.Filter = "*.txt"; //watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged); watcher.EnableRaisingEvents = true;
watcher.InternalBufferSize = 64000;
label31.Text = "自动公告启动";
}
这样可以处理文本显示中文,不过Encoding.Default是当前系统设置的默认字符集编码方式,建议使用(@"C:\MSSQLTips\" + e.Name, Encoding.GetEncoding("gb2312")),通用
这样可以处理文本显示中文,不过Encoding.Default是当前系统设置的默认字符集编码方式,建议使用(@"C:\MSSQLTips\" + e.Name, Encoding.GetEncoding("gb2312")),通用
试过指定为gb2312,但是指定后为乱码. 默认的话就能正常.
建议两个都试试..