自动添加基于路径的命名空间嵌入式VB.NET资源 [英] Automatically add namespace based on path to embedded VB.NET resources

查看:293
本文介绍了自动添加基于路径的命名空间嵌入式VB.NET资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯在C#中嵌入的资源,我喜欢它会自动添加命名空间到嵌入式资源的方式。这可以让我做这样的事情:

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

不幸的是,如果你在一个VB.NET工程尝试同样的,你会得到编译错误,因为它试图把所有嵌入的资源投入到根命名空间。为了解决这个问题,我可以手动编辑 .vbproj 文件,像这样:

<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?

推荐答案

这是Visual Basic如何不使用的文件夹路径默认创建的命名空间的副作用。

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

个人,然后其他的特殊情况,你谈论我preFER没有全部在名称的附加文件夹路径它。我希望MS增加了另一个属性添加到文件资源允许的命名空间在未来的具体设置,但在那之前......

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.

创建资源DLL在C#中,并从那里读你的资源即可。作为一个VB开发者,我不会三思这样做是为了满足特定的目的。

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.

修改
或者......可以使用一个VBS文件为prebuild事件中,以模拟空间所需要的格式文件复制到一个新的资源目录。

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 

输出foldere必须已经存在(显然不能有版本文件夹名称)。

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

这篇关于自动添加基于路径的命名空间嵌入式VB.NET资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆