文件事务和Kernel32.Dll

时间:2018-04-04 09:36:57

标签: c# transactions

我想从kernel32.dll实现MoveFileTransacted方法。我将移动文件后,我必须调用数据库行提交。如何在TransactionScope中实现MoveFileTransacted?

我发现了这样的事情:

 [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool MoveFileTransacted([MarshalAs(UnmanagedType.LPWStr)]string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)]string lpNewFileName,
            MoveFileFlags dwFlags, IntPtr transaction);

更新

        private void Test()
        {
            var sF = @"C:\" + Guid.NewGuid();

            var dF = @"C:\NewFile";

            Console.WriteLine("Store");

            try
            {
                using (var fs = new FileStream(sf, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(Content, 0, Content.Length);
                }

                using (TransactionScope ts = new TransactionScope())
                {
                    IntPtr transaction = Manager.GetTransactionFromDTC();
                    if (transaction != IntPtr.Zero)
                    {
                        if (Manager.MoveFileTransacted(
                            sF, dF, Manager.MoveFileFlags.MOVEFILE_WRITE_THROUGH, transaction))
                        {
                            Commit();
                        }
                        else
                        {
                            throw new Win32Exception(Marshal.GetLastWin32Error());
                        }
                        Manager.CloseHandle(transaction);
                    }
                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }

MoveFileTransacted签名是否正确?我找到了类似这样的东西:codeproject.com/Articles/17421/WebControls但我有异常:与线程关联的当前事务上下文不是事务对象的有效句柄。 ERROR_CURRENT_TRANSACTION_NOT_VALID

0 个答案:

没有答案