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:
However Explorer reported the file as being 30Kb…
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?
Ok this is looking really bad. Back to Visual Studio, right-click the file in Solution Explorer -> Open With… -> Binary Editor.
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:
- All comments and regions are removed.
- The code structure will have been lost.
- Depending on your build configuration, some optimisation may have taken place.
- 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:
- 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…
- Click on the ‘Options…’ button in the top right hand corner of the window.
- In the ‘Actions’ tab select the following:
- 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!
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
Thanks man! You saved me.
I have tried the Solution 4. But does not work. Can you please help me?
Awesome, i downloaded Recuva and did exactly as you said in solution 4, and it worked. Time to commit xD
Thanks a lot!
Try to check in below location in your system.
C:UsersusernameMy DocumentsVisual Studio Backup FilesProjectFolder
tnx man . tnx . you saved me
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..
CAN I PAY YOU MILLION DOLLAR PLEASE?
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!!!!!!!
Thanks a ton! you just saved my intenship!
Solution 4 works for…thanks..Saved a lot’s of my effort…
Fantastic, I had to opt for Solution 4.
It worked and recovered my 8 hours of work.
Thanks a lot.
[…] looked online to see if anyone else had encountered this problem and someone had this. I tried all the solutions but none gave me any results except solution four (which was to download […]
Thankyou so much for this help. I used Recover my file software to get my xaml.
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.
Thanks a lot bro………….
Dude, thanks for the solution. Worked like a charm, and saved something like one week of work…
Solution 4 rocks!
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!
Saved a whole day of work! Thanks
Solution 4 worked perfectly
Thanks mate, solution 4 helped me out. Did not known about the options in Recuva!
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
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!
THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
YOU SAVED HOURS OF MY WORK!
Thank you !! I was able to recover my file.
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.
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 ?
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!
Thank You very much!!! You just saved my two days of Work π
Thank you! Solution 4 worked for me! To all others out there, don’t trust anything, backup backup backup!
Bro god gave me you hahahaha thank you very much
Thanks a lot for sharing this, it saved me 3 to 4 days of work!
Thanks to recuva … Its working for me
Solution 4 FTW!! Tks
Thank you so much!
Your description helps me to save my 6 hours work.
May the force be with you man.
OMG. Solution 4 works… I was gonna summon Shenron then this showed up. THANKS!!!
Most useful page on the internet, thanks a million Sam and Recuva!
sir where is solution
thank you> <
i met same trouble
and you save me!!!!!!!!!!!!!!!!!!!!!!!!!
Thanks a lot !! solution 4 worked for me
Thank u very much
You saved my life!! Thanks a ton!
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.
Almost crying when you showed up with the solution. Thank you! Solution 4 worked for me
Solution 4 really saved my day after a corrupted .chtml due to an overheaded crappy fx-8350 processor. Thanks a lot.
Thank you so much dear !
You Saved 1 day of my work….
Life saving ! Thanks !
Solution 4 saved me! Ty bro
That was a great help to made us discover “Recuva”. It really helped by saving our precious work. And thanks to you.
nice!! s.4 save me
ermahgerd! that saved my day. thanks so much!
Thank u sooooooooo much
I love you!!!!!!!!!!
Thank you so much Solution for work for me.
You saved my 2 days time.
Thank to God, you and RECUVA, it saved my work in last month…
I suggest everyone in my situation. Solution 4 works like a charm
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.
Thanks you are a gent.
This saved days of work, genius!
You rock!
Guys you saved my 800 lines of code!!
Thank you very very very very much. Solution number 4 is worked for me.
Awesome man!
Thanks!
thanks a lot worked for me
I LOVE YOU!!!
God Bless you!!!
no more words…
Awesome mate! Solution 1 did it for me π
Wow! that was awesome! Thank you for complete and perfect description. solution 4 worked for me!
HOLA MUCHAS GRACIASSS ME SALVASTE MAN
GOOD JOB
You saved my time and day bro. I am simply not in a mood tore-write the code looking at 25KB file without text. Second option you provided saved my day.
Thank you once again.
#4 Worked! THANKS!
Thank you very much. Almost lost my biggest/ most important script.
thank u
Saved my arse, thanks!
Thanks Alot.
Thank you so much bro
Thank you man , solution 4 worked for me.
Thanks so much !
Nothing had worked so far. Backup folders were empty. There was no recovery popup (this time, I’ve seen it popup and it has worked a couple of times).
I even already tried Recuva (waited almost 1 hour for the deep scan…) but only an old deleted file with the same name was found, but I had no use for that one.
So, I was about to give up untill I read through this ‘story’ and bingo ! It worked !
CHEERS !
You save my A$$. I thank the good LAWD for people like yourself!
My work laptop updated while my project was open. The next time I opened my laptop, I noticed exactly the same, an empty notepad at script launch and a stream of null on notepad++. I was sad to realise that I have to rewrite something I worked on for 2-3 days, but then again, it’s a opportunity to re-do something and make it even better (at the cost of losing some time).
But then I came across your solution, and I was fortunately able to recover the file.
Many thanks for sharing the solution! Following solution 4 with the options highlighted in Recuva resolved the issue.
Very helpful.. Thank you so much. saved my time.
Thanks a lot!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Man! You have saved me a lot of time. Thumbs up. I was on the verge of giving up! Once again thank you!
Solution 1 β Decompile
solved my problem tanks π
Solution 4 saved me hours of rewriting code. Thanks!
work for me thanx
Thanks man. This article is very helpful. I successfully recovered the file.
just cant the tutoial in 2012 actually work in 2023 haha.
Thank You