检索带有特殊字符的嵌入资源
我在获取嵌入式资源的流时遇到问题。大多数在线示例显示的路径可以通过将路径的斜杠更改为源的点来直接转换(MyFolder/MyFile.ext 变为 MyNamespace.MyFolder.MyFile.ext)。但是,当文件夹名称中包含点并且使用特殊字符时,手动获取资源名称将不起作用。我试图找到一个函数,可以将路径转换为资源名称,因为 Visual Studio 在编译时重命名它们。
这些名称来自解决方案...
- Content/jQuery.UI-1.8.2/jQuery.UI.css
- 脚本/jQuery-1.5.2/jQuery.js
- Scripts/jQuery.jPlayer-2.0.0/jQuery.jPlayer.js
- Scripts/jQuery.UI-1.8.2/jQuery.UI.js
... 更改为这些名称资源...
- 内容.jQuery.UI_1._8._2.jQuery.UI.css
- 脚本.jQuery_1._5._2.jQuery.js
- 脚本.jQuery.jPlayer_2._0._0.jQuery.jPlayer.js
- 脚本.jQuery.UI_1 ._8._12.jQuery.UI.js
斜杠被转换为点。但是,当在文件夹名称中使用点时,第一个点显然被视为扩展名,其余点将更改为带有下划线前缀。不过,此逻辑不适用于 jQuery.js 文件,也许是因为“扩展名”是单个数字?这是一个能够翻译我迄今为止遇到的问题的函数,但在 jQuery.js 路径上不起作用。
protected String _GetResourceName( String[] zSegments )
{
String zResource = String.Empty;
for ( int i = 0; i < zSegments.Length; i++ )
{
if ( i != ( zSegments.Length - 1 ))
{
int iPos = zSegments[i].IndexOf( '.' );
if ( iPos != -1 )
{
zSegments[i] = zSegments[i].Substring( 0, iPos + 1 )
+ zSegments[i].Substring( iPos + 1 ).Replace( ".", "._" );
}
}
zResource += zSegments[i].Replace( '/', '.' ).Replace( '-', '_' );
}
return String.Concat( _zAssemblyName, zResource );
}
有没有可以帮我改名字的功能?它是什么?或者我在哪里可以找到所有规则以便我可以编写自己的函数?感谢您能够提供的任何帮助。
I'm having a problem getting streams for embedded resources. Most online samples show paths that can be directly translated by changing the slash of a path to a dot for the source (MyFolder/MyFile.ext becomes MyNamespace.MyFolder.MyFile.ext). However when a folder has a dot in the name and when special characters are used, manually getting the resource name does not work. I'm trying to find a function that can convert a path to a resource name as Visual Studio renames them when compiling..
These names from the solution ...
- Content/jQuery.UI-1.8.2/jQuery.UI.css
- Scripts/jQuery-1.5.2/jQuery.js
- Scripts/jQuery.jPlayer-2.0.0/jQuery.jPlayer.js
- Scripts/jQuery.UI-1.8.2/jQuery.UI.js
... are changed into these names in the resources ...
- Content.jQuery.UI_1._8._2.jQuery.UI.css
- Scripts.jQuery_1._5._2.jQuery.js
- Scripts.jQuery.jPlayer_2._0._0.jQuery.jPlayer.js
- Scripts.jQuery.UI_1._8._12.jQuery.UI.js
Slashes are translated to dots. However, when a dot is used in a folder name, the first dot is apparently considered an extension and the rest of the dots are changed to be prefixed with an underscore. This logic does not apply on the jQuery.js file, though, maybe because the 'extension' is a single number? Here's a function able to translate the issues I've had so far, but doesn't work on the jQuery.js path.
protected String _GetResourceName( String[] zSegments )
{
String zResource = String.Empty;
for ( int i = 0; i < zSegments.Length; i++ )
{
if ( i != ( zSegments.Length - 1 ))
{
int iPos = zSegments[i].IndexOf( '.' );
if ( iPos != -1 )
{
zSegments[i] = zSegments[i].Substring( 0, iPos + 1 )
+ zSegments[i].Substring( iPos + 1 ).Replace( ".", "._" );
}
}
zResource += zSegments[i].Replace( '/', '.' ).Replace( '-', '_' );
}
return String.Concat( _zAssemblyName, zResource );
}
Is there a function that can change the names for me? What is it? Or where can I find all the rules so I can write my own function? Thanks for any assistance you may be able to provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个很晚的答案......但由于这是谷歌上的第一次点击,我会发布我发现的内容!
您可以简单地强制编译器根据需要命名嵌入资源;这将从头开始解决这个问题...您只需编辑您的 csproj 文件,如果您想要其中包含通配符,通常会这样做!这就是我所做的:
在这种情况下,我告诉 Visual Studio,我希望将“某个文件夹”中的所有文件作为嵌入式资源导入。我还希望它们显示在 VS 解决方案资源管理器中的“某个文件夹”下(这是链接标签)。最后,在编译它们时,我希望它们的名称和地址与我磁盘上的名称和地址完全相同,只有“somefolder:\”前缀。最后一部分是施展魔法。
This is kinda a very late answer... But since this was the first hit on google, I'll post what I've found!
You can simply force compiler to name the embedded resource as you want it; Which will kinda solves this problem from the beginning... You've just got to edit your csproj file, which you normally do if you want wildcards in it! here is what I did:
In this case, I'm telling Visual studio, that I want all the files in "some folder" to be imported as embedded resources. Also I want them to be shown under "some folder", in VS solution explorer (this is link tag). And finally, when compiling them, I want them to be named exactly with same name and address they had on my disk, with only "somefolder:\" prefix. The last part is doing the magic.
这就是我想出的解决问题的办法。我仍然对更好的方法持开放态度,因为这有点像黑客(但似乎符合当前规范)。该函数需要处理 Uri 中的一个段(处理 Web 请求时为 LocalPath)。示例调用如下..
示例调用..
This is what I came up with to solve the issue. I'm still open for better methods, as this is a bit of a hack (but seems to be accurate with the current specifications). The function expects a segment from an Uri to process (LocalPath when dealing with web requests). Example call is below..
Example call..
Roel,
嗯...这是一个 hack,但我想它应该可以工作。只需在每个包含资源的目录中定义一个空的“Marker”类,然后获取其类型的 FullName,从末尾和 wala 中删除类名:这就是您的解码路径。
string path = (new MarkerClass()).GetType().FullName.Replace(".MarkerClass", "");
我确信有一种“更好”的方法来做到这一点......还有很多行代码;这个的优点是微软在改变东西时会维护它;-)
干杯。基思.
Roel,
Hmmm... This is a hack, but I guess it should work. Just define an empty "Marker" class in each directory which contains resources, then get the FullName of it's type, remove the class name from end and wala: there's your decoded-path.
string path = (new MarkerClass()).GetType().FullName.Replace(".MarkerClass", "");
I'm sure there's a "better" way to do it... with a LOT more lines of code; and this one has the advantage that Microsoft maintains it when they change stuff ;-)
Cheers. Keith.
这里的答案也很晚,在我自己尝试之前我用谷歌搜索了,最终我不得不这样做。
这是我想出的解决方案:
A late answer here as well, I googled before I attempted this on my own and I eventually had to.
Here's the solution I came up with: