/// <summary> /// Writes the data from source file to target file generically. /// </summary> /// <param name="sourcePath">The source path.</param> /// <param name="targetPath">The target path.</param> /// <param name="copyDataDelegate">The delegate.</param> private static void WriteTag(string sourcePath, string targetPath, CopyDataDelegate copyDataDelegate) { using (FileStream inputStream = File.Open(sourcePath, FileMode.Open, FileAccess.ReadWrite)) { using (FileStream outputStream = File.OpenWrite(targetPath)) { // Invoke the code that copies sound data from inputStream to outputStream // and injects new tag data copyDataDelegate.Invoke(inputStream, outputStream); } } }
/// <summary> /// Writes the tag data generically. /// </summary> /// <param name="path">The path.</param> /// <param name="copyDataDelegate">The delegate.</param> private static void WriteTag(string path, CopyDataDelegate copyDataDelegate) { // This implementation writes new content to a temp file and then replaces // the original file from the temp file string tempPath = Path.GetTempFileName(); try { WriteTag(path, tempPath, copyDataDelegate); // Save old file as bak // Rename new file as old // Delete bak file string backupPath = path + ".bak"; if (File.Exists(backupPath)) { File.Delete(backupPath); } File.Move(path, backupPath); File.Move(tempPath, path); File.Delete(backupPath); } catch { if (File.Exists(tempPath)) { File.Delete(tempPath); } throw; } }