GetPathRoot() –a cautionary tale….

This morning, whilst making a few changes to my dotnet-passbook library, I inadvertently ruined my PC by erasing files from my C drive. I want to pass on my tale of woe so others don’t fall into the same trap!

In response to an issued raised in GitHub, I decided to offer the ability to clean up temporary files after my library had generated a Pass. To do this, I had to make a few small changes to expose the path to the temporary file. As part of my OSS project, I have a sample web application that can be used to generate sample passes. I decided I’d try my code within my sample project.

I ended up with a piece of code like this:

    return new FileContentResult(generatedPass.GetPackage(), "application/");
    Directory.Delete(Path.GetPathRoot(generatedPass.Path), true);

.csharpcode, .csharpcode pre
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
background-color: #f4f4f4;
width: 100%;
margin: 0em;
.csharpcode .lnum { color: #606060; }

Sadly, I didn’t realise that GetPathRoot would return C:

To compound matters, the ApplicationPool running my sample code was my own account, so it had administrator privileges!

Needless to say, when I requested the pass, my HDD light started flickering. Unfortunately I didn’t realise what was happening. Since the request was taking so long (deleting all my files is a time consuming business), I just cancelled it. I then tried to fire up VS and got an error. I took a peek at my source code folder and found all the files were gone!

Then the penny dropped.

I fired up system restore and restored the system back to an earlier date, hoping to undo the damage I had caused.

Alas, it was in vein!


My laptop is beyond help. I have a feeling that I’ll need to reinstall Windows 8 from scratch…

I think there are two lessons here. Firstly, don’t run ApplicationPools with accounts that had administrator privileges. Secondly, read the documentation or intelli-sense when using methods that can be destructive. I’m surprised that Windows allowed my account to do this sort of damage, since I get a UAC elevation request whenever I try to change the system. Maybe ApplicationPools side step that?