Resource file and extracting add-in for visual studio

Resource file and extracting add-in for visual studio

  • Comments 2

Recently I joined a lab project and assigned with a boring task “find all hard-coded strings scattered in the source files which need to be localized, extract them into resource file”…

Obviously, this is a must-do task but few people willing to do it, I don’t want to ruin my reputation and accept this task happily, nobody will keep happy when he or she got to know there’re hundreds of files to be processed, me either…
But I decide to finish it smartly, then I think about the write a visual studio add-in to help me “FIND” string candidate and “REPLACE” with correct code automatically, finally I spent 1.5 days to get the add-in work and use another .5 day to finished this task, it’s not bad, if I manually do it, one week might be wasted and a lot of mistakes may appear also…

Creating a VS Addin project is easy from visual studio template, here is an abstract for this add-in, I name it as Thresher:

Search (CTRL+1):

  • define a regular expression to match strings in source file
  • don’t match the strings which appear in the comments, I will search it backward to see if there’s “//” appearing at the beginning of current line
  • loop the search when user keep it by “CTRL+1”, my strategy is generate strings collection, and use a index to loop each one.

Replace (CTRL+2):

  • extract the resource string and add it to Resources.resx
  • how about if there’s no Resources.resx in the project? Thresher will create one and add it to the project
  • generate the right method call statement and replace the resource string with it

Replace With Key (CTRL+3)

  • this option is same as #2, only difference is #2 generate resource key from the given resource string but #3 will pop up a dialog to ask user input a key…

Some problems I faced during the process, may it help you:


  • Why my Addin can’t be loaded by visual studio?
    Since I develop Thresher in visual studio 2010 and use it visual studio 2008, VS will not load the addin if the version marked in the .addin file doesn’t match VS version, I change the version from 10.0 to 9.0, it loads successfully…
  • Why visual studio doesn’t reload my Addin after I fixed the error in the .addin file?
    When I copied the test version of Thresher.addin to another machine’s addin folder, VS failed to load my addin since the wrong DLL path specified in .addin file, then VS will remove it, I fixed the path error in the .addin file, and restart VS, but it doesn’t reload my addin at all, it’s still disabled, I thought it may be caused by any cache that I don’t know, I searched the %appdata% and didn’t find any clue, I restart the computer, it’s silent still…
    the last solution I tried is: “move thresher.addin file out of addin folder, restart VS, move thresher.addin back the addin folder again, restart VS”, this time, it reloads! I don’t know why VS behavior in this way……
  • Why VS doesn’t generate new Resources.Designer.cs again when I update Resources.resx?
    if there’s no default Resources.resx, Thresher will add one. At first, I use ResxFileReader/ResxFileWriter to add a new .resx file and also attach a .Designer.cs to it, but I found whatever I make changes to the .resx file, it never auto-generate each Resource property stub in the Designer.cs file, I thought there must be some configuration in the .csproj to indicate VS to generate it, I compared two versions, the differences are, if you add Resources.resx manually, VS will add following configuration to project file:
  • <Compile Include="Properties\Resources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>Resources.resx</DependentUpon>
    </Compile>

    I’m trying to update .csproj file when Thresher add new Resources files to the project, but later, I found there’s more efficient way to resolve this problem…
    I guess VS will do something when I manually click “…create one” on the Resources tab of project Properties page, I started procmon.exe (from sysinternals.com) and found VS will find a template located under %VSInstallation%\Common7\IDE\ItemsTemplate\ResourceInternal.zip, I think I found the right solution, I copied Resource.vstemplate and Resource.resX to my Thresher folder and let Thresher call Properties.ProjectItems.AddTemplate(“Resource.vstemplate”, Resources.resx), in this way, it will act the same behavior as we add resources file manually from properties page which means it will add above XML section correctly into .csproj…

  • where to put the addin files?
    this is not a problem but a note, I didn’t generate a installer, just put the files under c:\users\<user>\documents\visual studio 2008\Addins (if no addins folder, please create one), this is for win7/2008 user, other OS, please reference MSDN.

I think it’s a nice way to handle the boring job, if you’re working on the similar task now, please check it out, it might help you…download it from the attachment!



Attachment: ThresherInstaller2008.vsi
Leave a Comment
  • Please add 2 and 6 and type the answer here:
  • Post
  • register your addin folders to

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\X.0\AutomationOptions\LookInFolders]

    so all user can load it.

  • thanks for the tip, Mao.

Page 1 of 1 (2 items)