Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检索具有特殊字符的嵌入式资源_C#_.net_Resources_Naming Conventions - Fatal编程技术网

C# 检索具有特殊字符的嵌入式资源

C# 检索具有特殊字符的嵌入式资源,c#,.net,resources,naming-conventions,C#,.net,Resources,Naming Conventions,我在获取嵌入式资源的流时遇到问题。大多数联机示例显示的路径可以通过将源路径的斜杠更改为点来直接转换(MyFolder/MyFile.ext变为MyNamespace.MyFolder.MyFile.ext)。但是,当文件夹名称中有一个点并且使用特殊字符时,手动获取资源名称不起作用。我试图找到一个函数,它可以在Visual Studio编译时重命名路径,从而将路径转换为资源名称 这些名称来自解决方案 Content/jQuery.UI-1.8.2/jQuery.UI.css Scripts/jQu

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

这些名称来自解决方案

  • 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
  • 。。。在参考资料中更改为这些名称

  • 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
  • 斜线被转换为点。但是,当在文件夹名称中使用点时,第一个点显然被视为扩展名,其余点则被更改为以下划线作为前缀。但是,这个逻辑不适用于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 );
        }
    
    protectedstring\u GetResourceName(String[]zSegments)
    {
    String zResource=String.Empty;
    for(int i=0;i
    是否有一个函数可以为我更改名称?这是怎么一回事?或者在哪里可以找到所有规则,以便编写自己的函数?感谢您提供的任何帮助。

    Roel

    嗯。。。这是一个黑客,但我想它应该会起作用。只需在包含资源的每个目录中定义一个空的“Marker”类,然后获取其类型的全名,从end中删除类名,然后wala:这是您的解码路径

    string path=(new MarkerClass()).GetType().FullName.Replace(“.MarkerClass”)

    我相信有一个“更好”的方法来做这件事。。。有更多的代码行;这一款的优点是,当微软更换产品时,它会保持不变;-)


    干杯。基思。

    这就是我解决这个问题的方法。我仍然对更好的方法持开放态度,因为这有点像黑客(但在当前规范中似乎是准确的)。该函数需要处理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 );
        }
    

    这是一个有点晚的回答。。。但由于这是谷歌的第一次成功,我将发布我的发现

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

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

    在本例中,我告诉VisualStudio,我希望“某个文件夹”中的所有文件都作为嵌入式资源导入。我还希望它们显示在VS解决方案资源管理器的“某个文件夹”下(这是链接标记)。最后,在编译它们时,我希望它们的名称与磁盘上的名称和地址完全相同,只有“somefolder:\”前缀。最后一部分是做魔术。

    这也是一个迟来的答案,我在自己尝试之前用谷歌搜索过,最终不得不这么做

    以下是我提出的解决方案:

        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);
        }
    
    publicsstringprocessfolderdash(字符串路径)
    {
    int dotCount=path.Split(“/”).Length-1;//获取斜杠的计数
    int dotCountLoop=1;//占位符
    string[]absolutepath=path.Split('/');
    for(int i=0;iif(dotCountLoop)感谢Keith的快速响应。我找不到提到的类,尽管它似乎在“Microsoft.Dss.Services.AssemblyEmbeddedResourceService”中(本地没有)。这通常也意味着mono没有它的实现,这是我不能忽略的。也许还有其他方法?我重申:在每个包含资源的目录中定义一个空的“Marker”类。我只是编了一个名称“MarkerClass”.我有点像google,前提是必须有更好的方法,并提出了这个小宝石:…我希望这很有用。你不能在名称空间中用点定义类(这正是我需要的信息,嵌入式资源名称是如何编码的?)。一旦名称正确,我就可以根据您的链接引用简单地使用GetManifestResourceStream。我不知道您不能在路径中包含点的目录中定义类…我猜您一直在试图找出Microsoft的规则,以便您可以自己对路径进行编码。唉。
    
        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);
        }