/// <summary>
/// Write a file with transaction
/// </summary>
/// <param name="data">Data to write</param>
/// <param name="path">Path to file</param>
/// <returns>Statut de l'opration</returns>
public static bool WriteStateFileTransacted(string path, XmlSerializer stateSerializer, AtomState atomState, System.Transactions.Transaction transaction)
{
if (atomState == null)
{
return(false);
}
SafeTransactionHandle txHandle = null;
SafeFileHandle fileHandle = null;
bool response = true;
try
{
IKernelTransaction kernelTx = (IKernelTransaction)TransactionInterop.GetDtcTransaction(transaction);
kernelTx.GetHandle(out txHandle);
fileHandle
= CreateFileTransacted(
path
, SafeTransactionHandle.FileAccess.GENERIC_WRITE
, SafeTransactionHandle.FileShare.FILE_SHARE_NONE
, IntPtr.Zero
, SafeTransactionHandle.FileMode.CREATE_ALWAYS
, 0
, IntPtr.Zero
, txHandle
, IntPtr.Zero
, IntPtr.Zero);
if (fileHandle.IsInvalid)
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}
using (FileStream stateFile = new FileStream(fileHandle, FileAccess.Write, 1024, false))
{
stateSerializer.Serialize(stateFile, atomState);
}
}
catch
{
transaction.Rollback();
response = false;
}
finally
{
if (fileHandle != null && !fileHandle.IsInvalid)
{
fileHandle.Close();
fileHandle.Dispose();
}
if (txHandle != null && !txHandle.IsInvalid)
{
txHandle.Close();
txHandle.Dispose();
}
}
return(response);
}