c#关于复合文档的问题

mayang_117 2008-07-14 03:03:38
我用如下代码写的复合文档文件,但用DocFile Viewer打不开,但我却能用程序按照我自己定的结构正确访问到写入的数据,请问这是怎么回事呀,这说明我生成的文档有错误吗??

public sealed class Storage : IDisposable
{
private bool disposed;
private IStorage storage;

public Storage(IStorage storage)
{
this.storage = storage;
}

~Storage()
{
//this.Dispose();
}

public static int IsStorageFile(string name)
{
return StorageApi.StgIsStorageFile(name);
}

public static Storage CreateDocFile(string storageFile, StorageMode mode)
{
IStorage storage = StorageApi.StgCreateDocfile(storageFile, (uint)mode, 0);

return new Storage(storage);
}

public static Storage Open(string storageFile, StorageMode mode)
{
try
{
IStorage storage = StorageApi.StgOpenStorage(storageFile, IntPtr.Zero, (uint)mode, IntPtr.Zero, 0);
return new Storage(storage);
}
catch (COMException)
{
return null;
}


}

public void CopyTo(Storage destinationStorage)
{
this.storage.CopyTo(0, IntPtr.Zero, IntPtr.Zero, destinationStorage.storage);
}

public Storage OpenStorage(string name, bool autoCreate)
{
IStorage subStorage;

try
{
this.storage.OpenStorage(name, null, (uint)(StorageMode.ReadWrite | StorageMode.ShareExclusive), IntPtr.Zero, 0, out subStorage);
}
catch (COMException e)
{

subStorage = null;
}

if (subStorage == null)
{
if (autoCreate)
return CreateStorage(name);

return null;
}

return new Storage(subStorage);
}

public Storage RecurOpenStorage(string name, bool autoCreate)
{
string pwcsName;

int pos = name.IndexOf('\\');
if (pos > 0)
{
pwcsName = name.Substring(0, pos);
name = name.Substring(pos + 1);
}
else
{
pwcsName = name;
name = "";
}

Storage subStorage = OpenStorage(pwcsName, autoCreate);
if (subStorage != null && name.Length > 0)
{
return subStorage.RecurOpenStorage(name, autoCreate);
}

return subStorage;
}

public void Dispose()
{
if (!this.disposed)
{
Marshal.ReleaseComObject(this.storage);
this.storage = null;

this.disposed = true;
}

GC.SuppressFinalize(this);
}

public Storage CreateStorage(string name)
{
IStorage subStorage = null;

try
{
//this.storage.OpenStorage(name, null,
// (uint)(StorageMode.ReadWrite | StorageMode.ShareExclusive),
// IntPtr.Zero, 0, out subStorage);

this.storage.CreateStorage(name,
(uint)(StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive),
0, 0, out subStorage);
this.storage.Commit(0);

return new Storage(subStorage);
}
catch (COMException)
{
if (subStorage != null)
Marshal.ReleaseComObject(subStorage);
}

return null;
}

public Stream CreateStream(string name)
{
IStream subStream = null;

try
{
//this.storage.OpenStream(name, IntPtr.Zero,
// (uint)(StorageMode.ReadWrite | StorageMode.ShareExclusive),
// 0, out subStream);

//if (subStream != null)
// this.storage.DestroyElement(name);

//Now create the element
this.storage.CreateStream(name,
(uint)(StorageMode.Create | StorageMode.ReadWrite | StorageMode.ShareExclusive),
0, 0, out subStream);
this.storage.Commit(0);

return new Stream(subStream);
}
catch (COMException)
{
if (subStream != null)
Marshal.ReleaseComObject(subStream);

return null;
}
}

public Stream OpenStream(string name,bool autoCreate)
{
IStream subStream;

try
{
this.storage.OpenStream(name, IntPtr.Zero,
(uint)(StorageMode.ReadWrite | StorageMode.ShareExclusive),
0, out subStream);


}
catch (COMException)
{
subStream=null;
}

if (subStream == null)
{
if (autoCreate)
return CreateStream(name);
return null;
}

return new Stream(subStream);

}

public void Commit(uint grfCommitFlags)
{
this.storage.Commit(grfCommitFlags);
}
}

...全文
185 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,618

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧