chinhdo / txFileManager

.NET Transactional File Manager is a .NET Standard library that allows you to enlist file operations (file/folder copies, writes, deletes, appends, etc.) in distributed transactions.
MIT License
111 stars 13 forks source link

Move (file rename) transaction is not rollbacked when program is exited with Ctrl+C (console application) #28

Open janseris opened 3 years ago

janseris commented 3 years ago

,NET 5.0

Source:

namespace NTFSTransactions
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo folder = new DirectoryInfo("testFiles");
            FileInfo[] files = folder.GetFiles();
            AtomicRename(files.ToList());
        }

        static void AtomicRename(List<FileInfo> files)
        {
            IFileManager fm = new TxFileManager();
            using (TransactionScope transactionScope = new TransactionScope())
            {
                foreach (var file in files)
                {
                    fm.Move("testFiles/" + file.Name, "testFiles/" + file.Name + "_sss");
                    Thread.Sleep(1000);
                    Console.WriteLine(file.Name + " renamed");
                }
                transactionScope.Complete();
            }
        }
    }
}

Rollback works if I return from the function before transactionScope.Complete(); is called. But it doesn't work if the application is closed (Ctrl+C or Environment.Exit(0)); Is there any way that it could roll back if application is closed (process ends)?