Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// Read a file with transaction
        /// </summary>
        /// <param name="path">Path to file</param>
        /// <returns>Donnes lu</returns>
        public object ReadFileTransacted(string path)
        {
            if (!File.Exists(path))
            {
                return(null);
            }

            SafeTransactionHandle txHandle   = null;
            SafeFileHandle        fileHandle = null;
            object raw = null;

            try
            {
                IKernelTransaction kernelTx = (IKernelTransaction)TransactionInterop.GetDtcTransaction(System.Transactions.Transaction.Current);
                kernelTx.GetHandle(out txHandle);

                fileHandle
                    = CreateFileTransacted(
                          path
                          , SafeTransactionHandle.FileAccess.GENERIC_READ
                          , SafeTransactionHandle.FileShare.FILE_SHARE_READ
                          , IntPtr.Zero
                          , SafeTransactionHandle.FileMode.OPEN_ALWAYS
                          , 0
                          , IntPtr.Zero
                          , txHandle
                          , IntPtr.Zero
                          , IntPtr.Zero);

                if (fileHandle.IsInvalid)
                {
                    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
                }

                using (FileStream stream = new FileStream(fileHandle, FileAccess.Read, 1024, false))
                {
                    BinaryFormatter reader = new BinaryFormatter();
                    raw = reader.Deserialize(stream);
                }
            }
            catch
            {
                raw = null;
            }
            finally
            {
                if (fileHandle != null && !fileHandle.IsInvalid)
                {
                    fileHandle.Close();
                    fileHandle.Dispose();
                }

                if (txHandle != null && !txHandle.IsInvalid)
                {
                    txHandle.Close();
                    txHandle.Dispose();
                }
            }
            return(raw);
        }
        public static bool RemoveDirectory(string path)
        {
            string message = string.Empty;

            return(TransactionActionHelper.DoActionWithCheckOnTransaction((ref string s) =>
            {
                if (!TransactionActionHelper.CheckConditions((ref string mes) =>
                {
                    if (!Directory.Exists(path))
                    {
                        Transaction.Current.Rollback();
                        message = "Wrong path or directory not exists";
                        return false;
                    }
                    return true;
                }, ref s))
                {
                    return false;
                }
                SafeTransactionHandle txHandle = null;
                try
                {
                    new TxFileManager().DeleteDirectory(path);
                }
                catch (Exception ex)
                {
                    message = ex.ToString();
                    Transaction.Current.Rollback();
                    return false;
                }
                return true;
            }, ref message));
        }
Beispiel #4
0
 protected static extern bool MoveFileTransacted(
     [In] String lpExistingFileName,
     [In] String lpNewFileName,
     [In] IntPtr lpProgressRoutine,
     [In] IntPtr lpData,
     [In] SafeTransactionHandle.Move dwFlags,
     [In] SafeTransactionHandle hTransaction
     );
Beispiel #5
0
 protected static extern bool CopyFileTransacted(
     [In] String lpExistingFileName,
     [In] String lpNewFileName,
     [In] IntPtr lpProgressRoutine,
     [In] IntPtr lpData,
     [In] bool pbCancel,
     [In] SafeTransactionHandle.Copy dwCopyFlags,
     [In] SafeTransactionHandle hTransaction
     );
Beispiel #6
0
        public static bool DeleteFiles(FileArgs args)
        {
            bool   response = true;
            string message  = String.Empty;

            return(TransactionActionHelper.DoActionWithCheckOnTransaction((ref string s) =>
            {
                foreach (var file in args.Files)
                {
                    if (!response)
                    {
                        return false;
                    }
                    if (!TransactionActionHelper.CheckConditions((ref string mes) =>
                    {
                        if (!File.Exists(file))
                        {
                            mes = "Wrong path or file exists";
                            Transaction.Current.Rollback();
                            return false;
                        }
                        return true;
                    }, ref s))
                    {
                        return false;
                    }
                    SafeTransactionHandle txHandle = null;
                    try
                    {
                        IKernelTransaction kernelTx =
                            (IKernelTransaction)TransactionInterop.GetDtcTransaction(Transaction.Current);
                        kernelTx.GetHandle(out txHandle);
                        if (!DeleteFileTransacted(file, txHandle))
                        {
                            Transaction.Current.Rollback();
                            response = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        response = false;
                        s = ex.Message;
                        Transaction.Current.Rollback();
                    }
                    finally
                    {
                        if (txHandle != null && !txHandle.IsInvalid)
                        {
                            txHandle.Dispose();
                        }
                    }
                }
                return response;
            }, ref message));
        }
Beispiel #7
0
 protected static extern SafeFileHandle CreateFileTransacted(
     [In] String lpFileName,
     [In] SafeTransactionHandle.FileAccess dwDesiredAccess,
     [In] SafeTransactionHandle.FileShare dwShareMode,
     [In] IntPtr lpSecurityAttributes,
     [In] SafeTransactionHandle.FileMode dwCreationDisposition,
     [In] int dwFlagsAndAttributes,
     [In] IntPtr hTemplateFile,
     [In] SafeTransactionHandle txHandle,
     [In] IntPtr miniVersion,
     [In] IntPtr extendedOpenInformation
     );
Beispiel #8
0
        private static SafeFileHandle CreateFileHandled(string path, ref string message)
        {
            SafeTransactionHandle txHandle   = null;
            SafeFileHandle        fileHandle = null;

            try
            {
                IKernelTransaction kernelTx =
                    (IKernelTransaction)TransactionInterop.GetDtcTransaction(Transaction.Current);
                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 (Path.GetExtension(path) == ".xaml")
                {
                    WriteToFile(Properties.Resources.Template, path, ref message, fileHandle);
                }
                if (fileHandle.IsInvalid)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;
                Transaction.Current.Rollback();
            }
            finally
            {
                if (txHandle != null)
                {
                    txHandle.Dispose();
                }
            }
            return(fileHandle);
        }
Beispiel #9
0
 public static bool CopyFileTo(string path, string pathCopy, ref string message)
 {
     return(TransactionActionHelper.DoActionWithCheckOnTransaction((ref string s) =>
     {
         if (!TransactionActionHelper.CheckConditions((ref string mes) =>
         {
             if (!File.Exists(path) || File.Exists(pathCopy))
             {
                 mes = "Wrong path or file exists";
                 Transaction.Current.Rollback();
                 return false;
             }
             return true;
         }, ref s))
         {
             return false;
         }
         bool response = false;
         SafeTransactionHandle txHandle = null;
         try
         {
             IKernelTransaction kernelTx =
                 (IKernelTransaction)TransactionInterop.GetDtcTransaction(Transaction.Current);
             kernelTx.GetHandle(out txHandle);
             response = CopyFileTransacted(path, pathCopy, IntPtr.Zero, IntPtr.Zero, false,
                                           SafeTransactionHandle.Copy.COPY_FILE_FAIL_IF_EXISTS, txHandle);
         }
         catch (Exception ex)
         {
             s = ex.Message;
             Transaction.Current.Rollback();
         }
         finally
         {
             if (txHandle != null)
             {
                 txHandle.Dispose();
             }
         }
         return response;
     }, ref message));
 }
        private static bool CreateDir(string path, ref string message)
        {
            bool response = false;
            SafeTransactionHandle txHandle = null;

            try
            {
                IKernelTransaction kernelTx =
                    (IKernelTransaction)TransactionInterop.GetDtcTransaction(Transaction.Current);
                kernelTx.GetHandle(out txHandle);
                response = CreateDirectoryTransacted(IntPtr.Zero, path, IntPtr.Zero, txHandle);
            }
            catch (Exception ex)
            {
                message  = ex.ToString();
                response = false;
                Transaction.Current.Rollback();
            }
            return(response);
        }
Beispiel #11
0
 internal TxFile(SafeTransactionHandle txHandle)
 {
     TxHandle = txHandle;
 }
Beispiel #12
0
 internal TxDirectory(SafeTransactionHandle txHandle)
 {
     TxHandle = txHandle;
 }
Beispiel #13
0
 protected static extern bool DeleteFileTransacted(
     [In] String lpFileName,
     [In] SafeTransactionHandle hTransaction
     );
 protected static extern bool RemoveDirectoryTransacted(
     [In] String lpPathName,
     [In] SafeTransactionHandle hTransaction
     );
 protected static extern bool CreateDirectoryTransacted(
     [In] IntPtr lpTemplateDirectory,
     [In] String lpNewDirectory,
     [In] IntPtr lpSecurityAttributes,
     [In] SafeTransactionHandle hTransaction
     );