ironfede / openmcdf

Microsoft Compound File .net component - pure C# - netstandard 2.0
Mozilla Public License 2.0
308 stars 76 forks source link

RenameItem doesn't seem to work #116

Closed Ferroreson closed 7 months ago

Ferroreson commented 7 months ago

I'm attempting to rename a CFStorage within a CFStorage. The root CFStorage is called "Relays". Within Relays is one CFStorage that I'm attempting to rename.

I pass in the Compound File as an array of bytes to ReadFileBytes, then call the RenameSettingFileEntry, then call GetRDBFileData() and write the byte array to a file. The "RenameItem" function runs without error but doesn't rename the CFStorage. Thanks for your help!

    private string DeviceRootPath = "Relays";
    private MemoryStream rdbMemoryStream;
    private CompoundFile rdbCompoundFile;

    private void ReadFileBytes(byte[] fileData)
    {
        rdbMemoryStream = new MemoryStream(fileData);  
        if (rdbCompoundFile != null)
        {
            rdbCompoundFile.Close();
            rdbCompoundFile = null;
        }
        rdbCompoundFile = new CompoundFile(rdbMemoryStream, CFSUpdateMode.ReadOnly, CFSConfiguration.Default);
    }
    public void RenameSettingFileEntry(string OldName, string NewName)
    {
        var DeviceRootEntry = (CFStorage)rdbCompoundFile.GetAllNamedEntries(DeviceRootPath).First();
        DeviceRootEntry.RenameItem(OldName, NewName);
    }
    public byte[] GetRDBFileData()
    {
        MemoryStream s = new MemoryStream();
        rdbCompoundFile.Save(s);

        return s.ToArray();
    }
Ferroreson commented 7 months ago

Bug isn't with OpenMCDF.

ironfede commented 7 months ago

This should work. You can change a storage from its parent storage.

CompoundFile cf = new CompoundFile();
cf.RootStorage.AddStorage("AStorage")
    .AddStream("AStream")
    .SetData(Helpers.GetBuffer(100));

cf.SaveAs("Hello$File");
cf.Close();

CompoundFile cf1 = new CompoundFile("Hello$File", CFSUpdateMode.Update, CFSConfiguration.Default);
try
{
    cf1.RootStorage.RenameItem("AStorage", "NewStorage");
    cf1.Commit();
    cf1.Close();
}
catch (Exception ex)
{
   Assert.Fail(ex.Message);
}

Kind Regards, Federico

Ferroreson commented 7 months ago

Thank you!!

On Tue, Feb 27, 2024, 3:30 PM Federico Blaseotto @.***> wrote:

This should work. You can change a storage from its parent storage.

CompoundFile cf = new CompoundFile(); cf.RootStorage.AddStorage("AStorage") .AddStream("AStream") .SetData(Helpers.GetBuffer(100));

cf.SaveAs("Hello$File"); cf.Close(); CompoundFile cf1 = new CompoundFile("Hello$File", CFSUpdateMode.Update, CFSConfiguration.Default);try{ cf1.RootStorage.RenameItem("AStorage", "NewStorage"); cf1.Commit(); cf1.Close();}catch (Exception ex){ Assert.Fail(ex.Message);}

Kind Regards, Federico

— Reply to this email directly, view it on GitHub https://github.com/ironfede/openmcdf/issues/116#issuecomment-1967770522, or unsubscribe https://github.com/notifications/unsubscribe-auth/BGPIWSXROIJBCC4R74P2NXDYVZM7VAVCNFSM6AAAAABD27O2PGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRXG43TANJSGI . You are receiving this because you authored the thread.Message ID: @.***>