[英]Open SQLite3 file as string c#
我正准备在C#中以字符串形式打开SQLite格式3文件
string fileName = @"C:\Users\Test\Downloads\hello.sql";
using (BinaryReader b = new BinaryReader(File.Open(fileName, FileMode.Open),
Encoding.GetEncoding(1252)))
{
int pos = 0;
int length = (int)b.BaseStream.Length;
while (pos < length)
{
int v = b.ReadInt32();
textBox1.Text += (v);
pos += sizeof(int);
}
}
这是到目前为止我得到的代码,但是当我尝试打开它时,它冻结并且不会让你做任何事情,就好像它在试图打开它的恒定循环中一样。
如果您仍然对我要做什么感到困惑。 如果您要在该文件上单击鼠标右键,然后用记事本或notepad ++打开它,则可以看到文件的内容(字符串),这就是我想要在程序的字符串或文本框中显示的内容。
PS Im不想以SQLite格式打开它,我想以文本形式打开它
我不确定为什么要这样做,但是这种方法很有效:
string fileName = @"C:\Users\Test\Downloads\hello.sql";
textBox1.Text = File.ReadAllText(filename, Encoding.GetEncoding(1252));
或者,如果您想以字节值查看文件内容,则可以使用:
byte[] buffer = File.ReadAllBytes(filename);
textBox1.Text = BitConverter.ToString(buffer);
编辑:
此代码适用于winforms文本框。 我不只是检查0x00,而是使用.NET的char.IsControl函数检查所有控制字符并替换它们。
string fileName = @"C:\Users\Test\Downloads\hello.sql";
byte[] buffer = File.ReadAllBytes(fileName);
Encoding enc = Encoding.GetEncoding(1252);
char[] chars = enc.GetChars(buffer);
for (int n = 0; n < chars.Length; n++)
{
if (char.IsControl(chars[n])) chars[n] = '.';
}
textBox1.Text = new string(chars);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.