txFileManager icon indicating copy to clipboard operation
txFileManager copied to clipboard

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

Open janseris opened this issue 5 years ago • 0 comments

,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)?

janseris avatar Feb 02 '21 02:02 janseris