Jump to content

C# programming help


Krisztian1990
Go to solution Solved by Krisztian1990,

Recommended Posts

Hello, guys!

I'm working on a great tool, and I really need your help.

My program is operational, but there's a treeView1-related issue.

The BoxBuilder form has some buttons.

button1 is responsible for create a new -*.box archive.

Button5 is responsible for add file(s) to treeView1 component.

Button6 is responsible for make new folder (node)

button7 is responsible for make subfolder (childnode) if a Node is selected.

Problem:

I can create the archive, but unable to open it... My other tool that uses listView1 component is working and the game can use the archive...

Can you help me fixing my code?

Attached I uploaded all the necessary files.

I'm looking forward for your replies.

Thank you very much in advance.

 

 

The problem only can be found in the BoxBuilder.cs file

Edited by Krisztian1990
Link to comment
Share on other sites

  • Solution
Posted (edited)

Hello, guys again! 

I have updated my code, now it is 100% good for files, but still can not handle neither folders (created with button6) nor subfolders (created with button7)..

Here's the fixed code below:

private void WriteFilesToArchive(BinaryWriter writer, TreeNodeCollection nodes)
{
    // List of metadata containing information about files and directories
    List<(string filename, long offset, int size)> metaDataList = new List<(string, long, int)>();

    // Write content and collect metadata
    WriteContent(writer, nodes, metaDataList);

    // Starting offset of metadata
    long directoryOffset = writer.BaseStream.Position;

    // Write the number of files (4 bytes)
    writer.Write(metaDataList.Count);

    // Write all metadata
    foreach (var metaData in metaDataList)
    {
        // Filename (terminated with a null character)
        writer.Write(Encoding.ASCII.GetBytes(metaData.filename));
        writer.Write((byte)0);  // Null terminator at the end of the filename

        // File offset (4 bytes)
        writer.Write((int)metaData.offset);

        // File size (4 bytes)
        writer.Write((int)metaData.size);
    }

    // Finally, write the directory offset (4 bytes)
    writer.Write((int)directoryOffset);
}

// Recursive function to write file and directory contents
private void WriteContent(BinaryWriter writer, TreeNodeCollection nodes, List<(string filename, long offset, int size)> metaDataList)
{
    foreach (TreeNode node in nodes)
    {
        string fullPath = node.Tag.ToString();

        if (Directory.Exists(fullPath)) // Directory handling
        {
            // Recursive call for files and subdirectories within the directory
            WriteContent(writer, node.Nodes, metaDataList);
        }
        else if (File.Exists(fullPath)) // File handling
        {
            FileInfo fileInfo = new FileInfo(fullPath);

            // Write file content to the archive
            long currentOffset = writer.BaseStream.Position;
            using (FileStream fs = new FileStream(fullPath, FileMode.Open))
            {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    writer.Write(buffer, 0, bytesRead);
                }
            }

            // Add file metadata to the list
            metaDataList.Add((node.FullPath, currentOffset, (int)fileInfo.Length));
        }
    }
}

So the only problem I have is the folder (Node) - subfolder (childnode) issue.

Edited by Krisztian1990
code updated, but fix needed
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...