How to recover my source code?

31,883

Solution 1

Try to use any recovery tool (isn't important that it is cs file).

and you have to use external subversion control next time.

I've been using xp-dev.com. It has free plan. Try to google and you will find out more services like this.

EDIT: There mentioned about code decompile - it is good idea. Try to use ILSpy - open source tool.

Solution 2

Here are a few steps you can do

  • try to recover the files "physically" via the free Recuva tool.

  • try to recover them from your temporary files folder in the user and files directory of windows

  • Try to look inside the (Global) Assembly Cache and disassemble back your application to C# via ILSpy

Solution 3

I had exactly the same issue yesterday :)

Additionally my drive was encrypted, and when I opened the file with Notepad++ it was all filled with NULLs. The problem is, if your drive is encrypted, there is no way to recover the data (apart from reading IL code and converting it back to C# from the compiled library). But, thankfully, all I needed to do was to check out original version from git hub.

I hope you did use source control. If you didn't, well this would be a good lesson.

Solution 4

Did you ever compile it so you have a .dll/.exe file with the compiled class in it? If so, then you can use a decompiler such as dotPeek from JetBrains, or Reflector from RedGate to decompile the file and get the .cs code back.

You will be suprised how well preserved the source code is. Sometimes I find that the compiled->decompiled code is better looking than the code was before compilation...

Remember to use version control and backups, it does seem like there is something wrong with your disk.

Solution 5

This is because the operation delete has happened on the file, and the code or data in your file is interrupted in the interim stage. It's not either committed to source safe or retained. Try getting older version and you have to write code again.

This happens sometimes even if your hard disk is not crashed

Share:
31,883
Albert Gao
Author by

Albert Gao

Updated on September 18, 2022

Comments

  • Albert Gao
    Albert Gao over 1 year

    after a system crash down, one of my .cs file get lost.
    The size of the file is 18KB,
    when I tried to open it in VS2010, vs2010 auto opens it in NotePad, but the content is empty.
    I tried to open it in NotePad++, the content turns into bunches of black block with word "NULL" in it.
    I tried to "save as" in UTF8, but the file is empty as same.

    How to recover this lost .cs file? :(

    • Lucero
      Lucero almost 12 years
      Have you run a CHKDSK /F on the drive (will require a reboot if your data is on the system drive)? If that doesn't help, then the data is probably lost - you're using a VCS, are you? ;)
    • Admin
      Admin almost 12 years
    • Admin
      Admin almost 12 years
      do you have your program exe file?.NET Reflector v6 provides support for .NET Framework 4.0 assemblies. Download it
    • Admin
      Admin almost 12 years
      @Usher great idea with Reflector. But it is payment tool now so I recommend to use ILSpy - wiki.sharpdevelop.net/ILSpy.ashx
    • ta.speot.is
      ta.speot.is almost 12 years
      Visual Studio does retain some autorecover information. It's usually in C:\Users\USERNAME\Documents\Visual Studio 2010\Backup Files.
    • Momoro
      Momoro over 4 years
      - Now that .NET Reflector is a paid tool, there are many, many free versions that people have created, such as ILSpy, or dotPeek from JetBrains Though, if you are REALLY committed to this .cs file, then I'd just purchase .NET Reflector, because it has very, very good results. -BUT, that's just my opinion :)
  • HikeMike
    HikeMike almost 12 years
    Why would anyone post something like this if they could just svn revert (or equivalent) and it'd restore the file?
  • oleksii
    oleksii almost 12 years
    @DanielBeck the easiest way not to get into such situations is to commit often. My suggestion was to fix the root problem, rather than to fix a single failure. If one cannot update code from the repository, it is a much bigger trouble (given this is production code).
  • Albert Gao
    Albert Gao almost 12 years
    @oleksii , hmm, I finally recover nearly 90% of my code by decompiling DLL file, and I learn my lesson, now I tried to SVN my project to a server, thanks...
  • Rich Homolka
    Rich Homolka over 11 years
    Code compile/decompile as a Tidy operation, very nice
  • KwahuNashoba
    KwahuNashoba over 4 years
    Thanks for reminder about ILSpy, you literary saved my as :)