internal static extern bool MoveFileTransacted( [In] string lpExistingFileName, [In] string lpNewFileName, [In] IntPtr lpProgressRoutine, [In] IntPtr lpData, [In] MoveFileFlags dwFlags, [In] KtmTransactionHandle hTransaction);
// TODO - BCL match - add overload that has a 'searchOption' argument public static string[] GetFiles(string path, string searchPattern) { using (TransactionScope scope = new TransactionScope()) using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { string dirSpec = System.IO.Path.Combine(path, searchPattern); NativeMethods.Win32FindData findFileData; SafeFileHandle hFind = FindFirstFileTransacted(dirSpec, ktmTx, out findFileData); try { List <string> files = new List <string>(); // List all the other files in the directory. do { files.Add(findFileData.cFileName); }while (NativeMethods.FindNextFile(hFind, out findFileData)); int error = Marshal.GetLastWin32Error(); if (error != NativeMethods.ErrorNoMoreFiles) { NativeMethods.HandleCOMError(error); } scope.Complete(); return(files.ToArray()); } finally { // Ignore failures from this api just as the BCL does... NativeMethods.FindClose(hFind); } } }
public static bool Move(string sourceFileName, string destFileName) { using (TransactionScope scope = new TransactionScope()) using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { // Allow copying to different volumes and to replace existing files NativeMethods.MoveFileFlags moveFlags = NativeMethods.MoveFileFlags.MovefileCopyAllowed | NativeMethods.MoveFileFlags.MovefileReplaceExisting; bool status = NativeMethods.MoveFileTransacted( sourceFileName, destFileName, IntPtr.Zero, IntPtr.Zero, moveFlags, ktmTx); if (!status) { NativeMethods.HandleCOMError(Marshal.GetLastWin32Error()); } scope.Complete(); return(status); } }
public static bool Copy(string sourceFileName, string destFileName, bool overwrite) { using (TransactionScope scope = new TransactionScope()) using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { NativeMethods.CopyFileFlags copyFlags = NativeMethods.CopyFileFlags.CopyFileFailIfExists; if (overwrite) { copyFlags = 0; // TODO - Correctness - Which flag value is this really supposed to be? Works though... } bool pbCancel = false; bool status = NativeMethods.CopyFileTransacted( sourceFileName, destFileName, IntPtr.Zero, IntPtr.Zero, ref pbCancel, copyFlags, ktmTx); if (!status) { NativeMethods.HandleCOMError(Marshal.GetLastWin32Error()); } scope.Complete(); return(status); } }
public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share) { using (TransactionScope scope = new TransactionScope()) using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { NativeMethods.FileMode internalMode = TranslateFileMode(mode); NativeMethods.FileShare internalShare = TranslateFileShare(share); NativeMethods.FileAccess internalAccess = TranslateFileAccess(access); SafeFileHandle hFile = NativeMethods.CreateFileTransacted( path, internalAccess, internalShare, IntPtr.Zero, internalMode, 0, IntPtr.Zero, ktmTx, IntPtr.Zero, IntPtr.Zero); if (hFile.IsInvalid) { NativeMethods.HandleCOMError(Marshal.GetLastWin32Error()); } FileStream stream = new FileStream(hFile, access); scope.Complete(); return(stream); } }
internal static extern SafeFileHandle FindFirstFileTransacted( [In] string lpDirSpec, [In] FindexInfoLevels fInfoLevelId, [Out] out Win32FindData lpFindFileData, [In] FindexSearchOps fSearchOp, [In] IntPtr lpSearchFilter, [In] int dwAdditionalFlags, [In] KtmTransactionHandle hTransaction);
internal static extern bool CopyFileTransacted( [In] string lpExistingFileName, [In] string lpNewFileName, [In] IntPtr lpProgressRoutine, [In] IntPtr lpData, [In][MarshalAs(UnmanagedType.Bool)] ref bool pbCancel, [In] CopyFileFlags dwCopyFlags, [In] KtmTransactionHandle hTransaction);
public static KtmTransactionHandle CreateKtmTransactionHandle() { if (Transaction.Current == null) { throw new InvalidOperationException("Cannot create a KTM handle without Transaction.Current"); } return(KtmTransactionHandle.CreateKtmTransactionHandle(Transaction.Current)); }
internal static extern SafeFileHandle CreateFileTransacted( [In] string lpFileName, [In] FileAccess dwDesiredAccess, [In] FileShare dwShareMode, [In] IntPtr lpSecurityAttributes, [In] FileMode dwCreationDisposition, [In] int dwFlagsAndAttributes, [In] IntPtr hTemplateFile, [In] KtmTransactionHandle hTransaction, [In] IntPtr pusMiniVersion, [In] IntPtr pExtendedParameter);
private static SafeFileHandle FindFirstFileTransacted(string dirSpec, KtmTransactionHandle ktmTx, out NativeMethods.Win32FindData findFileData) { SafeFileHandle hFile = NativeMethods.FindFirstFileTransacted( dirSpec, NativeMethods.FindexInfoLevels.FindExInfoStandard, out findFileData, NativeMethods.FindexSearchOps.NameMatch, IntPtr.Zero, 0, ktmTx); if (hFile.IsInvalid) { NativeMethods.HandleCOMError(Marshal.GetLastWin32Error()); } return(hFile); }
public static void Delete(string path) { using (TransactionScope scope = new TransactionScope()) using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { bool status = NativeMethods.DeleteFileTransacted( path, ktmTx); if (!status) { // Match the BCL behavior and disregard non-existant files... int error = Marshal.GetLastWin32Error(); if (error != NativeMethods.ErrorFileNotFound) { NativeMethods.HandleCOMError(error); } } scope.Complete(); } }
internal static extern bool DeleteFileTransacted( [In] string lpFileName, [In] KtmTransactionHandle hTransaction);