static void Main(string[] args) { if (args == null || args.Length == 0) { Console.WriteLine("usage: peToText [path]fileName.ext"); return; } using (var host = new PeReader.DefaultHost()) { IModule /*?*/ module = host.LoadUnitFrom(args[0]) as IModule; if (module == null || module == Dummy.Module || module == Dummy.Assembly) { Console.WriteLine(args[0] + " is not a PE file containing a CLR module or assembly."); return; } PdbReader /*?*/ pdbReader = null; string pdbFile = Path.ChangeExtension(module.Location, "pdb"); if (File.Exists(pdbFile)) { Stream pdbStream = File.OpenRead(pdbFile); pdbReader = new PdbReader(pdbStream, host); } using (pdbReader) { string txtFile = Path.ChangeExtension(pdbFile, "txt"); using (var writer = new StreamWriter(txtFile)) { SourceEmitter csSourceEmitter = new SourceEmitter(writer, host, pdbReader); csSourceEmitter.Traverse((INamespaceDefinition)module.UnitNamespaceRoot); writer.Close(); } } } }
static void Main(string[] args) { if (args == null || args.Length == 0) { Console.WriteLine("usage: peToText [path]fileName.ext [noIL] [noStack]"); return; } bool noIL = args.Length >= 2; bool noStack = args.Length >= 3; using (var host = new HostEnvironment()) { IModule /*?*/ module = host.LoadUnitFrom(args[0]) as IModule; if (module == null || module is Dummy) { Console.WriteLine(args[0] + " is not a PE file containing a CLR module or assembly."); return; } PdbReader /*?*/ pdbReader = null; string pdbFile = Path.ChangeExtension(module.Location, "pdb"); if (File.Exists(pdbFile)) { using (var pdbStream = File.OpenRead(pdbFile)) { pdbReader = new PdbReader(pdbStream, host); } } using (pdbReader) { var options = DecompilerOptions.AnonymousDelegates | DecompilerOptions.Iterators | DecompilerOptions.Loops; if (noStack) { options |= DecompilerOptions.Unstack; } module = Decompiler.GetCodeModelFromMetadataModel(host, module, pdbReader, options); SourceEmitterOutputString sourceEmitterOutput = new SourceEmitterOutputString(); SourceEmitter csSourceEmitter = new SourceEmitter(sourceEmitterOutput, host, pdbReader, noIL, printCompilerGeneratedMembers: true); csSourceEmitter.Traverse(module); string txtFile = Path.ChangeExtension(pdbFile, "txt"); File.WriteAllText(txtFile, sourceEmitterOutput.Data, System.Text.Encoding.UTF8); } } }