14

I wish to get a list of all strings that are used in a .NET assembly including the “static” values that local variables are set to, parameters passed to methods, fields at set to, etc.

I recall from something I read a long time ago that a .NET assembly contains a tables of all strings it uses (or they can be "interned")– or am I just dreaming?

Using .NET Reflector is a good ideal (thanks thijs), I will also have a look at its API if no one comes up with an already written tool.

(This is so I can write a tool to check we have not missed any strings that should be translated. I could process the C# source code, however I will then have to cope with Strings that are split over many lines, etc.)

I have just thought, I wish to exclude strings passed into CodeFlowException(), etc., so this is already getting more complex.

PS: if you can think of a better set of tags, please retag this question.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
  • No need to write a tool, one already exists.... – Mitch Wheat Jun 02 '09 at 11:51
  • I do need a custom tool, as I need to do more processing on the strings once I got them. However I am more then happy to feed my tool with a list of strings that another tool outputs. – Ian Ringrose Jun 02 '09 at 12:33
  • Try loading your assembly into Reflector, if what reflector sees is good (enough) for you then you can access it using System.Reflection too. – thijs Jun 02 '09 at 13:19

5 Answers5

14

You can use SysInternals Strings tool to view the strings in executables and object files.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • Using this tool you'll also get stuff like "!This program cannot be run in DOS mode" which is not written in the source but added by the compiler. You'll also get guids and base64 encoded resources if you're unlucky. All that gives you loads of work to separate the crap from the actual strings you need to translate. – thijs Jun 02 '09 at 13:17
3

For those who want to extract all user strings from a .NET assembly (.exe or .dll) I wrote a simple utility which can do that (file ExtractExeNetStrings2.zip). The utility is written in C#.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
vladob
  • 47
  • 1
  • Neat, but why does it pick up "hotovo... " but not ".txt.", "Exception: ", "press a key...", "#US", etc. from its own assembled self? – Jason Kleban Oct 14 '15 at 20:13
2

Simple C# utility that extracts all user strings from a .NET assembly (.exe or .dll) by examining bits of a binary file.

The source code from vladob's answer can now be found on github here. The link from the original answer has been broken. vladob gave consent to this repost. Actually I gave consent to me.

vbelcik
  • 21
  • 2
2

You should be able to do this using reflection, take a look at this:

http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.isliteral.aspx

and

http://msdn.microsoft.com/en-us/library/system.reflection.fieldinfo.aspx

thijs
  • 3,445
  • 1
  • 27
  • 46
  • reflection? why do something the hard way! ;) – Mitch Wheat Jun 02 '09 at 11:50
  • because "strings" also gives all strings that you didn't write yourself? (Such as the names of functions and variables) – thijs Jun 02 '09 at 11:55
  • You may not want/need to localize them, but you might want to pull them up anyway to check them for naming standards, quality assurance, f-bombs, etc. – GWLlosa Jun 02 '09 at 12:07
  • 1
    Guys, the poster is asking for a way to get the strings so that they can be translated, not enforce naming standards or anything else... – Mitch Wheat Jun 02 '09 at 12:08
  • What about strings that are passed to methods etc. – Ian Ringrose Jun 02 '09 at 12:27
  • My previous comment was referring to "Why do something the hard way"... do it the hard way because the easy way isn't good enough... – thijs Jun 09 '09 at 07:43
0

In the end I wrote a very simple C# parser that found the strings in the source code. This solved the problem I had and let me add “comments” to the strings to help the translators.

The comments were just read from C# comments in a “magic format”

(Sorry the parser is not good enough to allow anyone else to use it, it only just about works on my C# code base)

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317