I was testing by making a file copy within the scope and then throwing and error, thinking it would revert the rename. It didn't revert :(
using System;
using System.IO;
using System.Threading;
using System.Transactions;
namespace TestingTransactionScope
{
class Program
{
static void Main(string[] args)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
File.Move(@"C:\file1.txt", @"C:\file1.txt.backup1");
// Do Operation 1
// Do Operation 2
//...
MyClass.ThrowError();
// if all the coperations complete successfully, this would be called and commit the trabsaction.
// In case of an exception, it wont be called and transaction is rolled back
scope.Complete();
}
}
catch (ThreadAbortException ex)
{
// Handle exception
}
}
}
class MyClass
{
public static void ThrowError()
{
throw new Exception("Something went wrong");
}
}
}