Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ListView读取二进制文件_C#_.net_File_Listview_Binary - Fatal编程技术网

C# 使用ListView读取二进制文件

C# 使用ListView读取二进制文件,c#,.net,file,listview,binary,C#,.net,File,Listview,Binary,我制作了这个包含listview的exe,所以当我打开一个二进制文件时,它在一列中显示文本指针,在另一列中显示文本字符串 我使用“for循环”来显示指针,但我不知道如何使用循环来显示文本字符串,所以我想使用的是循环指针,显示它指向的文本,并在每个文本之后的00点停止 并且是一个关于二进制文件结构的示例 二进制文件的前4个字节是指针/字符串的数量,接下来的4个字节*前4个字节是指针,其余的是文本字符串,每个字符串用00 00分隔,都是Unicode 那么,有谁能帮助我在字符串列中为每个指针显示字符

我制作了这个包含listview的exe,所以当我打开一个二进制文件时,它在一列中显示文本指针,在另一列中显示文本字符串

我使用“for循环”来显示指针,但我不知道如何使用循环来显示文本字符串,所以我想使用的是循环指针,显示它指向的文本,并在每个文本之后的00点停止

并且是一个关于二进制文件结构的示例

二进制文件的前4个字节是指针/字符串的数量,接下来的4个字节*前4个字节是指针,其余的是文本字符串,每个字符串用00 00分隔,都是Unicode

那么,有谁能帮助我在字符串列中为每个指针显示字符串吗

编辑:以下是打开二进制文件的按钮的代码:

        private void menuItem8_Click(object sender, EventArgs e)
    {
        textBox1.Text = "";
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Open File";
        ofd.Filter = "Data Files (*.dat)|*.dat|All Files (*.*)|*.*";
        if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            MessageBox.Show("File opened Succesfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            path = ofd.FileName;
            BinaryReader br = new BinaryReader(File.OpenRead(path));               
            int num_pointers = br.ReadInt32();
            textBox1.Text = num_pointers.ToString();
            for (int i = 0; i < num_pointers; i++)
            {
                br.BaseStream.Position = i * 4 + 4;
                listView1.Items.Add(br.ReadUInt32().ToString("X"));
            }
            br.Close();
            br = null;
        }
        ofd.Dispose();
        ofd = null;
    }
private void menuItem8\u单击(对象发送方,事件参数e)
{
textBox1.Text=“”;
OpenFileDialog ofd=新建OpenFileDialog();
ofd.Title=“打开文件”;
ofd.Filter=“数据文件(*.dat)|*.dat |所有文件(*.*)|*.”;
if(ofd.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
Show(“文件已成功打开!”,“Info”,MessageBoxButtons.OK,MessageBoxIcon.Information);
path=ofd.FileName;
BinaryReader br=新的BinaryReader(File.OpenRead(path));
int num_pointers=br.ReadInt32();
textBox1.Text=num_pointers.ToString();
for(int i=0;i
首先,在实例化BinaryReader时提供编码,如下所示:

BinaryReader br = new BinaryReader(File.OpenRead(path), Encoding.Unicode);
接下来,您需要保留一个读取偏移的列表。(以后仍可以将其添加到ListView):

列表偏移量=新列表();
for(int i=0;i
最后,遍历偏移列表并从文件中读取字符串。您可能希望将它们放入某些数据结构中,以便以后可以使用数据填充视图:

Dictionary<int,string> values = new Dictionary<int,string>();
for (int i = 0; i < offsets.Count; i++)
{
    int currentOffset = offsets[i];

    // If it is the last offset we just read until the end of the stream
    int nextOffset = (i + 1) < offsets.Count ? offsets[i + 1] : (int)br.BaseStream.Length;

    // Note: Under the supplied encoding, a character will take 2 bytes.
    // Therefore we will need to divide the delta (in bytes) by 2.
    int stringLength = (nextOffset - currentOffset - 1) / 2;

    br.BaseStream.Position = currentOffset;

    var chars = br.ReadChars(stringLength);
    values.Add(currentOffset, new String(chars));
}

// Now populate the view with the data...
foreach(int offset in offsets)
{
    listView1.Items.Add(offset.ToString("X")).SubItems.Add(values[offset]);
}
字典值=新字典();
对于(int i=0;i
花点时间和精力将代码(相关部分)发布到此处。添加的代码,如果您还需要其他内容,请发表评论。您是否发现将字节数组转换为字符串很困难?是的,特别是当有很多字符串时,我只需要一种方法来循环指针列,并在第2列中显示每个指针指向的字符串taht。thank you.fs未声明,并且
offset.Add(br.ReadInt32())
没有向listview中的指针列添加任何内容,因此我删除了
br.BaseStream.Position=i*4+4并且它按照您说的那样工作,谢谢,但是第二个代码仍然没有很好地得到:您可以用
br.BaseStream.Length
替换
fs
。(我相应地更新了答案。)仍然不起作用:我把它们添加到了按钮点击事件中,不是吗?它不显示字符串,谢谢您的时间:)
Dictionary<int,string> values = new Dictionary<int,string>();
for (int i = 0; i < offsets.Count; i++)
{
    int currentOffset = offsets[i];

    // If it is the last offset we just read until the end of the stream
    int nextOffset = (i + 1) < offsets.Count ? offsets[i + 1] : (int)br.BaseStream.Length;

    // Note: Under the supplied encoding, a character will take 2 bytes.
    // Therefore we will need to divide the delta (in bytes) by 2.
    int stringLength = (nextOffset - currentOffset - 1) / 2;

    br.BaseStream.Position = currentOffset;

    var chars = br.ReadChars(stringLength);
    values.Add(currentOffset, new String(chars));
}

// Now populate the view with the data...
foreach(int offset in offsets)
{
    listView1.Items.Add(offset.ToString("X")).SubItems.Add(values[offset]);
}