Запись в structured storage файл в C#
Привожу код чтения файлов:
Код:
IStorage Is;
int result = StgOpenStorage(@"D:\Thumbs.db", null, STGM.READWRITE | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out Is);
IEnumSTATSTG SSenum;
Is.EnumElements(0, IntPtr.Zero, 0, out SSenum);
System.Runtime.InteropServices.ComTypes.STATSTG[] SSstruct = new System.Runtime.InteropServices.ComTypes.STATSTG[1];
uint NumReturned;
do
{
SSenum.Next(1, SSstruct,
out NumReturned);
if (NumReturned != 0)
{
listBox1.Items.Add(SSstruct[0].pwcsName);
}
} while (NumReturned > 0);
IStream CatStream;
Is.OpenStream("Catalog", IntPtr.Zero, (uint)(STGM.READWRITE | STGM.SHARE_EXCLUSIVE), 0, out CatStream);
byte[] buf = new byte[1000];
uint count;
CatalogHeader CH = new CatalogHeader();
CatStream.Read(buf, (uint)Marshal.SizeOf(CH), out count);
BinaryReader head = new BinaryReader(new MemoryStream(buf));
CH.num1 = head.ReadInt16();
CH.num2 = head.ReadInt16();
CH.thumbCount = head.ReadInt32();
CH.thumbWidth = head.ReadInt32();
CH.thumbHeight = head.ReadInt32();
head.Close();
for (int i = 0; i < CH.thumbCount; i++)
{
CatalogItem CI = new CatalogItem();
CatStream.Read(buf, 16, out count);
CI.num1 = BitConverter.ToInt32(buf, 0);
CI.itemID = BitConverter.ToInt32(buf, 4);
CI.Modified = DateTime.FromFileTime(BitConverter.ToInt64(buf, 8));
StringBuilder temp = new StringBuilder(50);
do
{
CatStream.Read(buf, 2, out count);
temp.Append(BitConverter.ToChar(buf, 0));
} while (buf[0] != 0 || buf[1] != 0);
temp.Length--;
CI.filename = temp.ToString();
CatStream.Read(buf, 2, out count);
CI.num2 = BitConverter.ToInt16(buf, 0);
string name = listBox1.Items[i].ToString();
IStream ThumbStream;
Is.OpenStream(name, IntPtr.Zero, (uint)(STGM.READWRITE | STGM.SHARE_EXCLUSIVE), 0, out ThumbStream);
byte[] bits = new byte[1000];
ThumbStream.Read(bits, 12, out count);
BinaryWriter JPEGfile = new BinaryWriter(File.Open(CI.filename, FileMode.Create));
do
{
ThumbStream.Read(bits, 1000,
out count);
JPEGfile.Write(bits, 0, (int)count);
} while (count > 0);
JPEGfile.Close();
long position;
ThumbStream.Seek(12, (uint)STREAM_SEEK.STREAM_SEEK_SET, out position);
System.Runtime.InteropServices.ComTypes.STATSTG fileinfo;
ThumbStream.Stat(out fileinfo, 0);
byte[] BitmapData = new byte[fileinfo.cbSize - 12];
ThumbStream.Read(BitmapData, (uint)BitmapData.Length, out count);
MemoryStream BitmapStream = new MemoryStream(BitmapData);
Bitmap Jmap = new Bitmap(BitmapStream);
pictureBox1.Image = Jmap;
MessageBox.Show("Next?");
}
int result = StgOpenStorage(@"D:\Thumbs.db", null, STGM.READWRITE | STGM.SHARE_EXCLUSIVE, IntPtr.Zero, 0, out Is);
IEnumSTATSTG SSenum;
Is.EnumElements(0, IntPtr.Zero, 0, out SSenum);
System.Runtime.InteropServices.ComTypes.STATSTG[] SSstruct = new System.Runtime.InteropServices.ComTypes.STATSTG[1];
uint NumReturned;
do
{
SSenum.Next(1, SSstruct,
out NumReturned);
if (NumReturned != 0)
{
listBox1.Items.Add(SSstruct[0].pwcsName);
}
} while (NumReturned > 0);
IStream CatStream;
Is.OpenStream("Catalog", IntPtr.Zero, (uint)(STGM.READWRITE | STGM.SHARE_EXCLUSIVE), 0, out CatStream);
byte[] buf = new byte[1000];
uint count;
CatalogHeader CH = new CatalogHeader();
CatStream.Read(buf, (uint)Marshal.SizeOf(CH), out count);
BinaryReader head = new BinaryReader(new MemoryStream(buf));
CH.num1 = head.ReadInt16();
CH.num2 = head.ReadInt16();
CH.thumbCount = head.ReadInt32();
CH.thumbWidth = head.ReadInt32();
CH.thumbHeight = head.ReadInt32();
head.Close();
for (int i = 0; i < CH.thumbCount; i++)
{
CatalogItem CI = new CatalogItem();
CatStream.Read(buf, 16, out count);
CI.num1 = BitConverter.ToInt32(buf, 0);
CI.itemID = BitConverter.ToInt32(buf, 4);
CI.Modified = DateTime.FromFileTime(BitConverter.ToInt64(buf, 8));
StringBuilder temp = new StringBuilder(50);
do
{
CatStream.Read(buf, 2, out count);
temp.Append(BitConverter.ToChar(buf, 0));
} while (buf[0] != 0 || buf[1] != 0);
temp.Length--;
CI.filename = temp.ToString();
CatStream.Read(buf, 2, out count);
CI.num2 = BitConverter.ToInt16(buf, 0);
string name = listBox1.Items[i].ToString();
IStream ThumbStream;
Is.OpenStream(name, IntPtr.Zero, (uint)(STGM.READWRITE | STGM.SHARE_EXCLUSIVE), 0, out ThumbStream);
byte[] bits = new byte[1000];
ThumbStream.Read(bits, 12, out count);
BinaryWriter JPEGfile = new BinaryWriter(File.Open(CI.filename, FileMode.Create));
do
{
ThumbStream.Read(bits, 1000,
out count);
JPEGfile.Write(bits, 0, (int)count);
} while (count > 0);
JPEGfile.Close();
long position;
ThumbStream.Seek(12, (uint)STREAM_SEEK.STREAM_SEEK_SET, out position);
System.Runtime.InteropServices.ComTypes.STATSTG fileinfo;
ThumbStream.Stat(out fileinfo, 0);
byte[] BitmapData = new byte[fileinfo.cbSize - 12];
ThumbStream.Read(BitmapData, (uint)BitmapData.Length, out count);
MemoryStream BitmapStream = new MemoryStream(BitmapData);
Bitmap Jmap = new Bitmap(BitmapStream);
pictureBox1.Image = Jmap;
MessageBox.Show("Next?");
}