本文整理汇总了C#中System.IO.BinaryWriter.WriteStruct方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryWriter.WriteStruct方法的具体用法?C# BinaryWriter.WriteStruct怎么用?C# BinaryWriter.WriteStruct使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.BinaryWriter
的用法示例。
在下文中一共展示了BinaryWriter.WriteStruct方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CalculateChecksum
public void CalculateChecksum()
{
Checksum = 0;
using (var ms = new MemoryStream())
{
var writer = new BinaryWriter(ms);
var reader = new BinaryReader(ms);
writer.WriteStruct(this);
reader.BaseStream.Position = 0;
while (reader.BaseStream.Length > reader.BaseStream.Position)
Checksum += reader.ReadByte();
}
Checksum = ~Checksum;
Checksum = Checksum.EndianSwap();
}
示例2: Write
/// <summary>
/// Writes a <see cref="VersionResource"/> object to the current <see cref="Stream"/>.
/// </summary>
/// <param name="resource">
/// The <see cref="VersionResource"/> to write.
/// </param>
public void Write(VersionResource resource)
{
if (resource == null)
{
throw new ArgumentNullException(nameof(resource));
}
if (Stream.Length < resource.Size)
{
Stream.SetLength(resource.Size);
}
using (var stream = new SubStream(Stream, 0, Stream.Length, leaveParentOpen: true))
using (var writer = new BinaryWriter(stream, Encoding.Unicode))
{
// Write the version resource header
WriteHeader(
writer,
resource.Size,
resource.FixedFileInfo == null ? 0 : Marshal.SizeOf(typeof(VS_FIXEDFILEINFO)),
VersionDataType.Binary,
"VS_VERSION_INFO");
if (resource.FixedFileInfo != null)
{
writer.WriteStruct(resource.FixedFileInfo.Value);
}
writer.Align();
if (resource.VarFileInfo != null)
{
WriteHeader(writer, resource.VarFileInfoSize, 0, VersionDataType.Text, "VarFileInfo");
WriteVarFileInfo(writer, resource);
}
if (resource.StringFileInfo != null)
{
WriteHeader(writer, resource.StringFileInfoSize, 0, VersionDataType.Text, "StringFileInfo");
WriteStringFileInfo(writer, resource);
}
}
}
示例3: WriteHeader
private void WriteHeader(BinaryWriter writer, long length, long valueLength, VersionDataType binary, string key)
{
var header = new VersionHeader
{
Length = (ushort)length,
ValueLength = (ushort)valueLength,
Type = binary
};
writer.WriteStruct(header);
writer.WriteUnicodeString(key);
writer.Align();
}
示例4: SaveFile
public void SaveFile()
{
var filePath = Game.GameManager.SavePath + FileName;
var strm = System.IO.File.OpenWrite(filePath);
mFile.Position = 0;
using (BinaryWriter wr = new BinaryWriter(strm))
{
while (true)
{
string chunk = "";
try
{
chunk = GetChunk();
}
catch (Exception)
{
break;
}
if (chunk == "MOHD")
{
wr.Write(0x4D4F4844);
wr.Write(Marshal.SizeOf(mHeader));
wr.WriteStruct(mHeader);
SkipChunk();
continue;
}
if (chunk == "MOTX")
{
wr.Write(0x4D4F5458);
List<byte> textureData = new List<byte>();
foreach (var texture in TextureNames)
{
var bytes = Encoding.UTF8.GetBytes(texture);
textureData.AddRange(bytes);
var len = bytes.Length % 4;
if (len != 0)
textureData.AddRange(new byte[4 - len]);
}
wr.Write(textureData.Count);
wr.Write(textureData.ToArray());
SkipChunk();
continue;
}
}
}
strm.Close();
}
示例5: ValidateStream
public static bool ValidateStream(Stream stream, bool fixCommonBugs)
{
if (stream == null) throw new ArgumentNullException();
if (!stream.CanSeek) throw new ArgumentException("The stream does not support seeking.");
if (!stream.CanRead) throw new ArgumentException("The stream does not support reading.");
var currentPos = stream.Position;
var reader = new BinaryReader(stream, Encoding.ASCII);
// Read the header.
var header = (TGA_HEADER)reader.ReadStruct(typeof(TGA_HEADER));
if (header.identsize > 0)
stream.Seek(header.identsize, SeekOrigin.Current);
// Check if we know the image data type.
if (!Enum.IsDefined(typeof(TgaPixelFormat), header.PixelFormat)) throw new Exception("Unknown image data type.");
// Check if color map is needed by data type and load it if available.
if (header.PixelFormat == TgaPixelFormat.Indexed)
if (!header.HasColorMap)
throw new Exception("No color map is defined.");
else
reader.ReadBytes((header.colormapbits / 8) * header.colormaplength);
if (fixCommonBugs)
{
fixCommonBugs = false;
if (!header.HasColorMap && (header.PixelFormat != TgaPixelFormat.Indexed))
{
if (!stream.CanWrite)
throw new ArgumentException("Tga header contains a bug but can't be fixed since the stream does not support writing.");
if ((header.colormapstart != 0) || (header.colormaplength != 0))
{
stream.Position = currentPos;
header.colormapstart = 0;
header.colormaplength = 0;
var wr = new BinaryWriter(stream);
wr.WriteStruct(header);
fixCommonBugs = true;
}
}
}
stream.Position = currentPos;
switch (header.bits)
{
case 32:
case 24:
case 16:
return fixCommonBugs;
case 8:
// Color palettes are supported.
if (header.HasColorMap && (header.PixelFormat == TgaPixelFormat.Indexed))
return fixCommonBugs;
// Greyscale is supported.
if (header.PixelFormat == TgaPixelFormat.Greyscale)
return fixCommonBugs;
break;
}
throw new NotSupportedException("Stream does not appear to be a valid TGA stream.");
}
示例6: New
//создаем конфиг с дефолт данными
public static void New()
{
CfgDatas.CfgHdr = _ch;
CfgDatas.ConStr = _ccs;
using (BinaryWriter binWriter=new BinaryWriter(new FileStream(FileName,FileMode.CreateNew)))
{
binWriter.WriteStruct<CfgDataStruct>(CfgDatas);
}
//MessageBox.Show("Config create");
Load();
}