Re: Access denied when deleting a file in TestFixtureTearDown

"Tom Fanning" <[email protected]> Thu, 13 Nov 2008 21:07:18 +0000
Newsgroups gmane.comp.windows.dotnet.nunit.user
Message-ID <[email protected]>
My bad, the file originally had a read-only attribute set.

Sorry for the noise.

On Thu, Nov 13, 2008 at 8:59 PM, Tom Fanning <[email protected]> wrote:
> I am getting an UnauthorizedAccessException when calling File.Delete()
> on a file created as part of a TestFixtureSetup method.
>
> If I manually go and delete the given file, I have no problems - there
> are no locks, nothing is read only etc.
>
> Environment is .NET 2.0, Visual Studio 2008 SP1, nunit 2.4.7 (for
> historical reasons - can easily move forwards if necessary), Windows
> Server 2008 x64. My assembly is built for x86 only so runs in WoW
> "mode".
>
> Here's what I'm trying to do. Any ideas at all?
>
> Kind regards
> Tom
>
>    [TestFixture]
>    public class SchemaConsistencyChecks
>    {
>        private string baseFolder;
>        private bool tornDown = false;
>        [TestFixtureSetUp]
>        public void FixtureSetUp()
>        {
>            Console.WriteLine("Setting up SchemaConsistencyChecks.cs");
>            // find out where this DLL is. 3 levels back out is Solution Items.
>            string codeBase = Assembly.GetExecutingAssembly().CodeBase;
>            string dllLocation = codeBase.Replace("file:///",
> "").Replace("/", @"\");
>            string slnItems = Directory.GetParent(
>                                Directory.GetParent(
>                                    Directory.GetParent(
>
> Path.GetDirectoryName(dllLocation)).FullName).FullName).FullName;
>            string mdb = Path.Combine(slnItems, "elas.mdb");
>            string mdw = Path.Combine(slnItems, "elas.mdw");
>            try
>            {
>                string tempPath = Path.GetTempPath();
>                baseFolder = Path.Combine(tempPath, Guid.NewGuid().ToString());
>                Directory.CreateDirectory(baseFolder);
>                File.Copy(mdb, Path.Combine(baseFolder, Path.GetFileName(mdb)));
>                File.Copy(mdw, Path.Combine(baseFolder, Path.GetFileName(mdw)));
>            }
>            catch
>            {
>                FixtureTearDown();
>                throw;
>            }
>        }
>        [TestFixtureTearDown]
>        public void FixtureTearDown()
>        {
>            if (!tornDown)
>            {
>                tornDown = true;
>                Console.WriteLine("Tearing down SchemaConsistencyChecks.cs");
>                if (File.Exists(Path.Combine(baseFolder, "elas.mdb")))
>                    File.Delete(Path.Combine(baseFolder, "elas.mdb"));
>                if (File.Exists(Path.Combine(baseFolder, "elas.mdw")))
>                    File.Delete(Path.Combine(baseFolder, "elas.mdw"));
>                if (Directory.Exists(baseFolder))
>                    Directory.Delete(baseFolder);
>            }
>        }
>        [Test]
>        public void AccessDatabaseCopiedCorrectly()
>        {
>            Assert.That(File.Exists(Path.Combine(baseFolder, "elas.mdb")));
>        }
>    }
>

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/