Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically add namespace based on path to embedded VB.NET resources

I am used to embedding resources in C# and I like the way it automatically adds namespaces to the embedded resources. That allows me to do things like this:

files\version1\config.xml
files\version2\config.xml
files\version2\config.xml

Unfortunately, if you try the same in a VB.NET project you'll get compile errors, since it tries to put all embedded resources into the root namespace. To get around this I can manually edit the .vbproj file like so:

<EmbeddedResource Include="files\version1\config.xml">
  <LogicalName>$(RootNamespace).files.version1.config.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="files\version2\config.xml">
  <LogicalName>$(RootNamespace).files.version2.config.xml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="files\version3\config.xml">
  <LogicalName>$(RootNamespace).files.version3.config.xml</LogicalName>
</EmbeddedResource>

While this works, it's manual, time consuming and error prone so my question is this: Can a build task or build event be written do this automatically?

like image 741
Sean Gough Avatar asked Oct 12 '12 14:10

Sean Gough


1 Answers

This is a side effect of how Visual Basic does not use folder paths to create namespaces by default.

Personally, other then the specific case, your talking about I prefer it not having all the additional folder paths in the name. I hope MS adds another property to the file resources to allow a namespace to be set specifically in the future, but until then....

The solution is quite simple though.

Create a resource-only dll in C# and read your resources from there. As a VB developer I would not think twice about doing this to satisfy that specific purpose.

EDIT. OR... can use a vbs file as a prebuild event to copy files to a new resource directory in the format needed to make the simulated namespace.

dim fSys
set fsys=createobject("Scripting.FileSystemObject")
dim root : root= "c:\temp"
dim out : out="c:\temp\DynResource"

dim rFo: set rFo=fsys.getfolder(root)
dim outPath

for each sf in rFo.SubFolders
    if instr(1, sf.name, "Version")>=1 then 'valid resource folder
        for each f in sf.Files
            outpath = out & "\" & sf.name & "." & f.name
            if fsys.FileExists(output) then
                dim tf:set tf=fsys.getfile(output)
                if tf.length<>f.length or tf.DateLastModified<>f.DateLastModified then
                    f.copy outPath,true
                else
                    'same file, no update required.
                end if 
            else
                f.copy outPath,true
            end if
        next
    end if 
next 

Output foldere must already exist (and obviously cannot have "Version" in the folder name).

like image 80
DarrenMB Avatar answered Oct 22 '22 08:10

DarrenMB