【问题标题】:C# Combine two files to run via output file - File BinderC# 合并两个文件以通过输出文件运行 - File Binder
【发布时间】:2017-01-25 06:34:03
【问题描述】:

我正在尝试编写文件活页夹。

我将两个文件中的字节数组合并到输出文件中。

看来我是在合并这两个文件而不是绑定它们,因为我希望在运行输出文件时能够同时运行这两个文件。

有没有一种方法可以组合这两个数组,但在运行时拆分它们以便能够运行这两个文件?

或者还有其他方法可以编写文件活页夹吗?

代码:

    private void button3_Click(object sender, EventArgs e)
    {
        if(textBox1.Text != null & textBox2.Text !=null)
        {
            string filepath1 = this.textBox1.Text;
            byte[] Bytes1 = File.ReadAllBytes(filepath1);

            string filepath2 = this.textBox2.Text;
            byte[] Bytes2 = File.ReadAllBytes(filepath2);

            byte[] combinedbytes = new byte[Bytes1.Length + Bytes2.Length];

            Array.Copy(Bytes1, 0, combinedbytes, 0, Bytes1.Length);
            Array.Copy(Bytes2, 0, combinedbytes, Bytes1.Length, Bytes2.Length);

            File.WriteAllBytes(outfile, combinedbytes);

        }

【问题讨论】:

    标签: c# arrays file byte


    【解决方案1】:

    是的,这是可能的,但不能没有中间文件或中间结构。

    1. 您可以将两个字节数组的长度存储在单独的文件中。
    File.WriteAllLines(path, new[] { Bytes1.Length, Bytes2.Length });
    
    byte[] Bytes1 = new byte[File.ReadLines(path)[0]];
    byte[] Bytes2 = new byte[File.ReadLines(path)[1]];
    

    然后加载这 2 个文件并从合并的文件中执行 Array.Copy。分隔数组

    1. 使用一个文本(可以是二进制)文件并创建结构,这样您就可以知道合并的数组的长度及其内容。 JSON 应该适合你。
    {
        Bytes1: //Your bytes here,
        Bytes2: //Your second bytes here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多