I wrote a C# application for a client a couple of years ago, but I no longer have the source code. All I have is the EXE that I deployed on the client's PC. Is there a way I can generate C# source code from the EXE?
Reflector and its add-in FileDisassembler.
Reflector will allow to see the source code. FileDisassembler will allow you to convert it into a VS solution.
When Red Gate said there would no longer be a free version of .Net Reflector, I started using ILSpy and Telerik's JustDecompile. I have found ILSpy to decompile more accurately than JustDecompile (which is still in Beta). Red Gate has changed their decision and still have a free version of .Net Reflector, but now I like ILSpy.
From the ILSpy website (https://github.com/icsharpcode/ILSpy/):
ILSpy is the open-source .NET assembly browser and decompiler.
ILSpy Features
- Assembly browsing
- IL Disassembly
- Decompilation to C#
- Supports lambdas and 'yield return'
- Shows XML documentation
- Saving of resources
- Search for types/methods/properties (substring)
- Hyperlink-based type/method/property navigation
- Base/Derived types navigation
- Navigation history
- BAML to XAML decompiler
- Save Assembly as C# Project
- Find usage of field/method
- Extensible via plugins (MEF)
Update:
April 15, 2012, ILSpy 2.0 was released. New features compared with version 1.0:
- Assembly Lists
- Support for decompiling Expression trees
- Support for lifted operatores on nullables
- Decompile to Visual Basic
- Search for multiple strings separated by space (searching for "Assembly manager" in ILSpy.exe would find AssemblyListManager)
- Clicking on a local variable will highlight all other occurrences of that variable
- Ctrl+F can be used to search within the decompiled code view
Update:
- ILSpy 2.1 supports async/await decompilation
Reflector is no longer free in general, but they do offer it for free to open source developers: http://reflectorblog.red-gate.com/2013/07/open-source/
But a few companies like DevExtras and JetBrains have created free alternatives:
Reflector and the File Disassembler add-in from Denis Bauer. It actually produces source projects from assemblies, where Reflector on its own only displays the disassembled source.
ADDED: My latest favourite is JetBrains' dotPeek.
Telerik JustDecompile is free and has a feature to create projects from .NET assemblies.
I'm surprised no one has mentioned Microsoft's ildasm. It may not be as pretty as ILSpy or Reflector, but it comes with Visual Studio so many developers already have it.
To run it (assuming VS 2013, should be similar for other versions):
- Select Start > All Programs > Visual Studio 2013 > Visual Studio Tools.
- Double-click on Developer Command Prompt for VS2013.
- Run "ildasm" from the resulting command prompt.
- In the tool, select File > Open and open your executable or DLL.
Now you can navigate the DLL structure. Double-click on class members to see the IL. Use File > Dump to export IL to a file.
I'm surprised no one has mentioned dnSpy. dnSpy is a debugger and .NET assembly editor. You can use it to edit and debug assemblies even if you don't have any source code available.
Main features:
- Debug .NET and Unity assemblies
- Edit .NET and Unity assemblies
- Light and dark themes
It is open source and one of most widely used reverse engineering tool for dot net.
There are various .NET / C# Decompilers available nowadays (2022):
- ILSpy for everyone [free OSS C# IL multi-platforms]
- dotPeek for convenient decompilation [free C# IL]
- dnSpy for gurus, security and hackers [free OSS C# VB IL]
- JustDecompile for everyone [free OSS C# VB IL] (continued as CodemerxDecompile)
- IldAsm for nostalgia [free IL]
- .NET Reflector for history [commercial unsupported C# VB IL MC++]
My preference goes to ILSpy because it is free, OSS, very fast (compared to others), so well maintained it is even used by Visual Studio and decompiled code is accurate. This blog post In the Jungle of .NET Decompilers explains in details all these .NET decompilers.
CodemerxDecompile is the continuation of JustDecompile. It uses the same engine but updated for the latest versions of the .NET framework. And it is truly multi-platform running on Windows, Mac and Linux employing AvaloniaUI.
Sadly, JustDecompile has been abandoned for years now. There has been no release with new features since 2017. Lately the open source decompilation engine was removed from gitHub too.
The original team revived it with CodemerxDecompile stepping on the last public version of the open source decompilation engine and building on top of it.
© 2022 - 2024 — McMap. All rights reserved.