检索带有特殊字符的嵌入资源

发布于 2024-11-03 05:50:03 字数 1575 浏览 3 评论 0原文

我在获取嵌入式资源的流时遇到问题。大多数在线示例显示的路径可以通过将路径的斜杠更改为源的点来直接转换(MyFolder/MyFile.ext 变为 MyNamespace.MyFolder.MyFile.ext)。但是,当文件夹名称中包含点并且使用特殊字符时,手动获取资源名称将不起作用。我试图找到一个函数,可以将路径转换为资源名称,因为 Visual Studio 在编译时重命名它们。

这些名称来自解决方案...

  1. Content/jQuery.UI-1.8.2/jQuery.UI.css
  2. 脚本/jQuery-1.5.2/jQuery.js
  3. Scripts/jQuery.jPlayer-2.0.0/jQuery.jPlayer.js
  4. Scripts/jQuery.UI-1.8.2/jQuery.UI.js

... 更改为这些名称资源...

  1. 内容.jQuery.UI_1._8._2.jQuery.UI.css
  2. 脚本.jQuery_1._5._2.jQuery.js
  3. 脚本.jQuery.jPlayer_2._0._0.jQuery.jPlayer.js
  4. 脚本.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 ...

  1. Content/jQuery.UI-1.8.2/jQuery.UI.css
  2. Scripts/jQuery-1.5.2/jQuery.js
  3. Scripts/jQuery.jPlayer-2.0.0/jQuery.jPlayer.js
  4. Scripts/jQuery.UI-1.8.2/jQuery.UI.js

... are changed into these names in the resources ...

  1. Content.jQuery.UI_1._8._2.jQuery.UI.css
  2. Scripts.jQuery_1._5._2.jQuery.js
  3. Scripts.jQuery.jPlayer_2._0._0.jQuery.jPlayer.js
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴骨ら 2024-11-10 05:50:03

这是一个很晚的答案......但由于这是谷歌上的第一次点击,我会发布我发现的内容!

您可以简单地强制编译器根据需要命名嵌入资源;这将从头开始解决这个问题...您只需编辑您的 csproj 文件,如果您想要其中包含通配符,通常会这样做!这就是我所做的:

<EmbeddedResource Include="$(SolutionDir)\somefolder\**">
  <Link>somefolder\%(RecursiveDir)%(Filename)%(Extension)</Link>
  <LogicalName>somefolder:\%(RecursiveDir)%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>

在这种情况下,我告诉 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:

<EmbeddedResource Include="$(SolutionDir)\somefolder\**">
  <Link>somefolder\%(RecursiveDir)%(Filename)%(Extension)</Link>
  <LogicalName>somefolder:\%(RecursiveDir)%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>

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.

窝囊感情。 2024-11-10 05:50:03

这就是我想出的解决问题的办法。我仍然对更好的方法持开放态度,因为这有点像黑客(但似乎符合当前规范)。该函数需要处理 Uri 中的一个段(处理 Web 请求时为 LocalPath)。示例调用如下..

    protected String _GetResourceName( String[] zSegments )
    {
        // Initialize the resource string to return.
        String zResource = String.Empty;

        // Initialize the variables for the dot- and find position.
        int iDotPos, iFindPos;

        // Loop through the segments of the provided Uri.
        for ( int i = 0; i < zSegments.Length; i++ )
        {
            // Find the first occurrence of the dot character.
            iDotPos = zSegments[i].IndexOf( '.' );

            // Check if this segment is a folder segment.
            if ( i < zSegments.Length - 1 )
            {
                // A dash in a folder segment will cause each following dot occurrence to be appended with an underscore.
                if (( iFindPos = zSegments[i].IndexOf( '-' )) != -1 && iDotPos != -1 )
                {
                    zSegments[i] = zSegments[i].Substring( 0, iFindPos + 1 ) + zSegments[i].Substring( iFindPos + 1 ).Replace( ".", "._" );
                }

                // A dash is replaced with an underscore when no underscores are in the name or a dot occurrence is before it.
                //if (( iFindPos = zSegments[i].IndexOf( '_' )) == -1 || ( iDotPos >= 0 && iDotPos < iFindPos ))
                {
                    zSegments[i] = zSegments[i].Replace( '-', '_' );
                }
            }

            // Each slash is replaced by a dot.
            zResource += zSegments[i].Replace( '/', '.' );
        }

        // Return the assembly name with the resource name.
        return String.Concat( _zAssemblyName, zResource );
    }

示例调用..

    var testResourceName = _GetResourceName( new String[] {
        "/",
        "Scripts/",
        "jQuery.UI-1.8.12/",
        "jQuery-_.UI.js"
    });

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

    protected String _GetResourceName( String[] zSegments )
    {
        // Initialize the resource string to return.
        String zResource = String.Empty;

        // Initialize the variables for the dot- and find position.
        int iDotPos, iFindPos;

        // Loop through the segments of the provided Uri.
        for ( int i = 0; i < zSegments.Length; i++ )
        {
            // Find the first occurrence of the dot character.
            iDotPos = zSegments[i].IndexOf( '.' );

            // Check if this segment is a folder segment.
            if ( i < zSegments.Length - 1 )
            {
                // A dash in a folder segment will cause each following dot occurrence to be appended with an underscore.
                if (( iFindPos = zSegments[i].IndexOf( '-' )) != -1 && iDotPos != -1 )
                {
                    zSegments[i] = zSegments[i].Substring( 0, iFindPos + 1 ) + zSegments[i].Substring( iFindPos + 1 ).Replace( ".", "._" );
                }

                // A dash is replaced with an underscore when no underscores are in the name or a dot occurrence is before it.
                //if (( iFindPos = zSegments[i].IndexOf( '_' )) == -1 || ( iDotPos >= 0 && iDotPos < iFindPos ))
                {
                    zSegments[i] = zSegments[i].Replace( '-', '_' );
                }
            }

            // Each slash is replaced by a dot.
            zResource += zSegments[i].Replace( '/', '.' );
        }

        // Return the assembly name with the resource name.
        return String.Concat( _zAssemblyName, zResource );
    }

Example call..

    var testResourceName = _GetResourceName( new String[] {
        "/",
        "Scripts/",
        "jQuery.UI-1.8.12/",
        "jQuery-_.UI.js"
    });
玉环 2024-11-10 05:50:03

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.

最美不过初阳 2024-11-10 05:50:03

这里的答案也很晚,在我自己尝试之前我用谷歌搜索了,最终我不得不这样做。

这是我想出的解决方案:

    public string ProcessFolderDash(string path)
    {
        int dotCount = path.Split('/').Length - 1; // Gets the count of slashes
        int dotCountLoop = 1; // Placeholder

        string[] absolutepath = path.Split('/');
        for (int i = 0; i < absolutepath.Length; i++)
        {
            if (dotCountLoop <= dotCount) // check to see if its a file
            {
                absolutepath[i] = absolutepath[i].Replace("-", "_");
            }

            dotCountLoop++;
        }

        return String.Join("/", absolutepath);
    }

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:

    public string ProcessFolderDash(string path)
    {
        int dotCount = path.Split('/').Length - 1; // Gets the count of slashes
        int dotCountLoop = 1; // Placeholder

        string[] absolutepath = path.Split('/');
        for (int i = 0; i < absolutepath.Length; i++)
        {
            if (dotCountLoop <= dotCount) // check to see if its a file
            {
                absolutepath[i] = absolutepath[i].Replace("-", "_");
            }

            dotCountLoop++;
        }

        return String.Join("/", absolutepath);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文