Visual Studio Crashes and How to Recover a Corrupted .cs File

In spite of using source control, sometimes it’s still possible to lose work due to the quirks of computers. At least you can mitigate this and only suffer losing a few hours of work, but still, when those few hours were a stroke of genius, the likes of which cannot be reproduced, you can’t help but shed a tear when all is lost. But fear not, here are 4 techniques that you can try to recover your code with before crying.

Lately I’ve used VirtualBox to run Windows 8.1 on my Mac Book Air under Mavericks. My Parallels license expired and I wanted to check out the alternatives. So far it’s been a pleasure to use but recently I’ve experienced it falling over, taking OSX down with it. I’ve probably seen the Grey Screen of Death twice over the last 6 years of using a Mac but have seen it twice in the last week.

It’s generally not a problem, just a minor inconvenience, until last night. Just as I’m about to wrap up for the day after making some great progress, everything comes crashing down. Once everything is back up and running, I couldn’t open one of my .cs files. Visual Studio would launch Notepad.exe which would display a seemingly empty file, although I could still make text selections:

1. Empty Notepad

However Explorer reported the file as being 30Kb…

2. Filesize

This gave me hope, the data is still there! It’s probably just a case of a dodgy character somewhere in the file as a result of the crash. What does Notepad++ say?

4. Notepad++

Ok this is looking really bad. Back to Visual Studio, right-click the file in Solution Explorer -> Open With… -> Binary Editor.

3. Binary Content

Oh dear. This doesn’t look good. Apparently it’s a common problem and most likely due the crash occurring whilst that file was being written to on the disk, although in the 10 years that I’ve been working with Visual Studio this is the first time I’ve experienced it. There’s not much one can do to guard against this, other than diagnosing and fixing the cause for Virtualbox crashing, but that’s a whole new problem.

No point crying over spilt milk, let’s just get this fixed! First thing’s first, do not attempt to build your project again as we may need to use the output of the last build to recover your files!

Solution 1 – Decompile

If you have a recent build of your project, you can recover the code from the generated binaries using something like ILSpy (open source) or Dotpeek (free).

If you search around the web for ‘Visual Studio corrupted file’, this is most likely the first solution you’ll find and is often touted as the only solution. Whilst it’s a perfectly practical solution and perhaps your only option, for me it’s a last resort for the following reasons:

  1. All comments and regions are removed.
  2. The code structure will have been lost.
  3. Depending on your build configuration, some optimisation may have taken place.
  4. Doesn’t really help if the code has been obfuscated.

I’m sure you’ll agree it’s better than nothing. If you don’t have any assemblies or you want to try to restore the file to its original awesomeness, continue on.

Solution 2 – AutoRecovery

If you have AutoRecovery enabled, you might be lucky enough to find a backup of the file. Chances are that Visual Studio would’ve prompted you to restore the corrupted file when launching it after the crash. Anyway it’s still worth checking the backup folder for your project, in:

%USERPROFILE%DocumentsVisual Studio [Version]Backup Files[Solution Name]

No dice for me, this directory was empty for me, on to the next solution.

Solution 3 – CHKDSK

Depending on the particulars of the corruption, trusty old chkdsk might help. It might be wise to attempt Solution 4 first to minimise the chances of any data being overwritten.

From an elevated command prompt execute:

chkdsk /f

You’ll receive a message that the drive is locked and the operation will need to be performed on the next restart. Just say ok and restart your machine. Depending on your setup, it could take a while for this to run so be patient.

Solution 4

This was the solution that worked for me and allowed me to recover the full content of my file! I used the free recovery software Recuva. Either use this or whatever you prefer, although instructions here are for Recuva.

Whilst Recuva is primarily for recovering deleted files and this isn’t technically a deleted file, we’re facing a similar challenge. The file is corrupt and pointing to the wrong clusters on the HDD, thus when we load our file it’s loading garbage instead of the correct content.

My first process was:

  1. Launched Recuva and used the Wizard to scan the folder containing my source files. After a few minutes it showed me an empty results window. Not looking good…
  2. Click on the ‘Options…’ button in the top right hand corner of the window.
  3. In the ‘Actions’ tab select the following: 5. Recuva Options
  4. Hit the ‘Ok’ button.

At this point Recuva started updating the results view, but it still remained empty! Sad face. In a last-ditch effort I quit Recuva, navigated to the source directory in Explorer, right-click, ‘Scan for deleted files’.

Some more dialogs popped up, scanning the drive again until boom, a results view that displayed the contents of the folder, including all the sub-directories. This looks good!

I scrolled down to the file in question and read across the row: ‘Not deleted, Not overwritten’ etc. On the right of the screen there’s a tabbed preview pane. There was no preview available, the ‘Info’ tab looked normal but the ‘Header’ tab was where the magic was!

6. Recovered File

Right click the file in the results pane, ‘Recover Highlighted…’, save to desktop and bingo, fully recovered file. Thanks Recuva!

If you’re ever unlucky to encounter this issue, probably just as you’re about to pack up to go home or preparing for a big tech demo at an investor pitch, one of these solutions will get you unstuck.

If you have any other tips, share them in the comments and I’ll add them to the post.

Edit: Reader ‘bizh’ provided an additional step to Solution 4 if it doesn’t work out fully. I haven’t tested this so proceed at your own risk:

If Recuva is empty, you can delete the corrupted file and then run Recuva. In that case Recuva will definitely find the file and recover the clean version of it

Bizh

55 thoughts on “Visual Studio Crashes and How to Recover a Corrupted .cs File

  1. Awesome, i downloaded Recuva and did exactly as you said in solution 4, and it worked. Time to commit xD

    Thanks a lot!

  2. Try to check in below location in your system.

    C:UsersusernameMy DocumentsVisual Studio Backup FilesProjectFolder

  3. Awesome Solution..I did exactly same as you said in solution 4 and recovered corrupted .cs file..its worked..Awesome..you saved my 1 day work..

    thanks a lot..

  4. Luckily the Solution 2 worked for me and I only lost 7 lines of code that I had fresh on my head, so I wrote them again quickly.

    I got inspired while coding that, I was furious! You brought back my smile 🙂

    Thanks a lot!!!!!!!

  5. OMG you are freeeakin awesome!!! Kudos for hanging in there and figuring out a solution… and then being KIND Enough to Post It!!!

    Amazing Thank you!!

    Yes solution #4 worked, with hope restored when i saw some of the content in the header tab.

  6. OMG thanks! You saved me several hours of re-work after a .cs file was corrupted in the same exact manner you mentioned after a power outage of my computer. Step 4 worked for me after I went to the “Actions” tab and had it give me a listing of all files in the folder. I couldn’t believe that it was exactly where I left it right before the outage!

  7. Option 4 worked for me. You saved my life!

    If Recuva is empty , you can delete the corrupted file and then run Recuva, In that case Recuva will definitely find the file and recover the clean version of it

  8. Thanks bizh! I’ve added your comment to the main article.

    And thanks to everyone else for your comments, it’s great to know this has helped so many people!

  9. Thank you very much, I got the very similar Visual Studio 2015 Community crash in Windows 10. Recuva did recover my file, but only half of it. To find other part, I have to recover the compiled exe and use ILSpy.

  10. Very Strange. I had similar problem with HomeController.cs ,it did recover.but its show view code (html code) instead of controller code.

    Now what ?

  11. Thanks a lot for sharing this, it saved me 2 days of work!

    Solution 4 worked for me, the file only had the first bytes corrupt and was magically there again

    This is what happend in my case:

    – Shared clipboard in VirtualMachine stopped working

    – I thought instead of restart I try saving and restarting the VM with my project still open… to save time lol

    – The VM hangs at 100% at saving the state and I decide to kill the task after a while…

    – After rebooting, my main code file was corrupt… I tried to hold my frustration back, maybe there is still a backup file to undelete or something… I knew I could retrieve the code by decompiling… but that would be a latest option…

    – The first undelete software I grabbed from the web was concealed as “free”, but when you tried to undelete that file you wanted, knowing that any more time going ahead it could be overwritten….. you have to pay and register right after when you hit “recover”… how I hate such tactics… uninstalled

    – Then I squeezed google a little harder and found Recuva, but it only found a poor conditioned file from 1 day ago… not very promising, still I recovered it

    – Without looking at the recovered file I thought I go for another search, maybe other users had similiar problems or VisualStudio saves files at some other temporary place, who knows…

    – Then I found this page, and with Recuva still open I tried Solution 4 and of course it worked and I got my file back as it was 1 hour ago!!

    The last backup was 2 days ago, and in those 2 days I worked day and night on the code

    Thank you!

  12. Solution 1 worked for me…

    I’ve spent in my project at least 60 hours of my life… Damn neighbor, he provoked a power failure and, well… You can imagine the whole story.

    Just to say thanks Mr. Sam. I mean really really thanks with all my heart. God bless you (even if you aren’t a believer).

    Best regards and thanks again.

    K. J.

  13. Solution 4 really saved my day after a corrupted .chtml due to an overheaded crappy fx-8350 processor. Thanks a lot.

  14. Wow! Thank you for this information!

    Recuva saved an 8,000 line cpp file, which took months and months of development.

    And it all took about 3 minutes.

Leave a Reply

Your email address will not be published. Required fields are marked *