private void ReplaceFileContent(string fileName, byte[] bytes, bool firstAttempt)
{
try
{
using (FileStream fs = File.Create(fileName))
{
byte[] headerBytes = this.GetHeaderBytes();
if (headerBytes != null)
{
fs.Write(headerBytes, 0, headerBytes.Length);
}
fs.Write(bytes, 0, bytes.Length);
byte[] footerBytes = this.GetFooterBytes();
if (footerBytes != null)
{
fs.Write(footerBytes, 0, footerBytes.Length);
}
}
}
catch (DirectoryNotFoundException)
{
if (!this.CreateDirs || !firstAttempt)
{
throw;
}
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
//retry.
ReplaceFileContent(fileName, bytes, false);
}
}