如何将更改后的 COM 结构化存储文件写入磁盘?
我实现了一个 COM 结构化存储文件阅读器,可以打开存储和流对象,这很高兴。但现在我希望能够将内容从一个存档复制到另一个存档并重命名,然后将内容写回磁盘。我什至没有解决复制和重命名操作,因为我似乎无法将更改写入磁盘。我尝试调用 Root IStorage 对象的 Commit 方法,它似乎运行良好,但我没有看到任何变化。我通过 C#.NET 中的 PInvoke 实现此功能。以下是适用的代码元素:
public void Load(string path)
{
STATSTG[] storageElementInfos = new STATSTG[1];
// Populate our storage object
var result = StgOpenStorage(path, null, STGM.DIRECT | STGM.READWRITE | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out archive);
// Only proceed if we succeed in populating our SS object
if (result == 0)
{
uint count;
IEnumSTATSTG elements;
archive.EnumElements(0, IntPtr.Zero, 0, out elements);
elements.Next(1, storageElementInfos, out count);
if (count != 0)
{
if ((STGTY)storageElementInfos[0].type == STGTY.STGTY_STORAGE)
{
//create Root Here
var root = storageElementInfos[0];
Root = new StructuredStorageContainer();
Root.element = root;
(Root as StructuredStorageContainer).Load(archive);
}
}
}
}
public void Save()
{
if (Root is StructuredStorageContainer)
{
(Root as StructuredStorageContainer).Save();
}
archive.Commit(0);
}
进行更改
ListArchive((archive.Root as StructuredStorageContainer), 0);
archive.Root.Name = "Fail";
archive.Save();
StructuredStorageNode 类的名称定义。
internal System.Runtime.InteropServices.ComTypes.STATSTG element;
public string Name
{
get
{
return element.pwcsName;
}
set
{
element.pwcsName = value;
}
}
I have a COM Structured Storage File reader implemented that can open Storage and stream objects, that's all happy. But now I want to be able to copy things from one archive to another and rename things and then write things back to the disk. I haven't even adressed the copy and rename operations because I can't seem to even write changes to disk. I tried calling the Root IStorage object's Commit method and it seems to run fine, but I see no changes. I'm implementing this via PInvoke in C#.NET. Here are applicable code elements:
public void Load(string path)
{
STATSTG[] storageElementInfos = new STATSTG[1];
// Populate our storage object
var result = StgOpenStorage(path, null, STGM.DIRECT | STGM.READWRITE | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out archive);
// Only proceed if we succeed in populating our SS object
if (result == 0)
{
uint count;
IEnumSTATSTG elements;
archive.EnumElements(0, IntPtr.Zero, 0, out elements);
elements.Next(1, storageElementInfos, out count);
if (count != 0)
{
if ((STGTY)storageElementInfos[0].type == STGTY.STGTY_STORAGE)
{
//create Root Here
var root = storageElementInfos[0];
Root = new StructuredStorageContainer();
Root.element = root;
(Root as StructuredStorageContainer).Load(archive);
}
}
}
}
public void Save()
{
if (Root is StructuredStorageContainer)
{
(Root as StructuredStorageContainer).Save();
}
archive.Commit(0);
}
Making the Change
ListArchive((archive.Root as StructuredStorageContainer), 0);
archive.Root.Name = "Fail";
archive.Save();
The name definition on the StructuredStorageNode Class.
internal System.Runtime.InteropServices.ComTypes.STATSTG element;
public string Name
{
get
{
return element.pwcsName;
}
set
{
element.pwcsName = value;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道 P/Invoke 是否是您项目的设计请求,但如果不是,并且您需要一个快速解决方案,您可以尝试我的免费开源库来读取/写入结构化存储文件 OpenMCDF。
它不使用 P/Invoke 作为 100% C# .net 实现。
I don't know if P/Invoke is a design-request for your project but if it is not and you need a quick solution you can give a try to my free and open source library for reading/writing Structured Storage files OpenMCDF.
It doesn't use P/Invoke being a 100% C# .net implementation.