示例#1
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);
        }
示例#2
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);
        }
示例#3
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));
        }
示例#4
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);
        }
示例#5
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));
 }