sil
sil copied to clipboard
Clean up all disassembly
See original post at:
http://www.codeproject.com/Articles/602648/See-the-Intermediate-Language-for-Csharp-Code?msg=4837578#xx4837578xx
Just a note on the following code
public static DisassembledAssembly DisassembleAssembly(string assemblyPath)
{
// Create a disassembled assembly.
var disassembledAssembly = new DisassembledAssembly();
// Set the key properties.
disassembledAssembly.AssemblyPath = assemblyPath;
// Get the assembly name.
try
{
var assembly = Assembly.LoadFile(assemblyPath);
disassembledAssembly.ShortName = assembly.GetName().Name;
disassembledAssembly.FullName = assembly.GetName().FullName;
}
catch (Exception exception)
{
throw new InvalidOperationException(string.Format("Failed to load the assembly '{0}', it may not be a valid assembly file.", assemblyPath), exception);
}
// Create working directory.
string savepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + disassembledAssembly.ShortName;
if(!Directory.Exists(savepath))
{
Directory.CreateDirectory(savepath);
}
var tempFilePath = savepath + "\\" + disassembledAssembly.ShortName + ".il"; //Path.GetTempFileName();
// Create an ILDASM object.
var ildasm = new ILDASM();
ildasm.IncludeLineNumbers = true;
ildasm.IncludeOriginalSourceCode = true;
ildasm.InputFilePath = assemblyPath;
ildasm.OutputFilePath = tempFilePath;
// Disasseble the code.
try
{
ildasm.Run();
}
catch (Exception exception)
{
// Add detail and rethrow the exception.
throw new InvalidOperationException(string.Format("Failed to disassemble '{0}'.", assemblyPath), exception);
}
// Read the file.
using (var stream = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read))
using (var reader = new StreamReader(stream))
{
disassembledAssembly.RawIL = reader.ReadToEnd();
}
// Clean up the temporary file.
File.Delete(tempFilePath);
// We're done.
return disassembledAssembly;
}
The following line only removed the il their could be several resource files that are included that dose not get cleaned up. File.Delete(tempFilePath);
if you wanted to clean up all the file names you should loop through the files in that directory and match the first name of the file like as follows or something similer.
string filename = tempFilePath.Split('.')[0].Split('\\')[tempFilePath.Split('.')[0].Split('\\').Length -1];
string dir = Path.GetDirectoryName(tempFilePath);
foreach (string f in Directory.GetFiles(dir))
{
FileInfo fi = new FileInfo(f);
if(fi.Name.Split('.')[0] == filename)
{
File.Delete(f);
}
}
This will remove all the temp files that where generated.