Getting embedded resources with special characters

I have a problem with threads for embedded resources. Most online samples show paths that can be directly translated by changing the slash of the path to a point for the source (MyFolder / MyFile.ext becomes MyNamespace.MyFolder.MyFile.ext). However, when the folder has a period in the name and when special characters are used, manually obtaining the resource name does not work. I am trying to find a function that can convert the path to a resource name, since Visual Studio renames them at compilation time.

These names are from the solution ...

  • Contents /jQuery.UI-1.8.2/jQuery.UI.css
  • Scripting / jQuery -1.5.2 / jquery.js
  • Scripts /jQuery.jPlayer-2.0.0/jQuery.jPlayer.js
  • Scripts /jQuery.UI-1.8.2/jQuery.UI.js

... change to these names in 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 translate to points. However, when a dot is used in the folder name, the first dot appears to be considered an extension, and the remaining dots are changed to have an underscore prefix. However, this logic does not apply to the jQuery.js file, perhaps because the โ€œextensionโ€ is one number? Here is a function that can translate the problems that I have had so far, but does not work along 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 names for me? What is it? Or where can I find all the rules so that I can write my own function? Thanks for any help you can provide.

+7
source share
4 answers

This is what I came up with to solve the problem. I'm still open to better methods, as it is a bit hacky (but seems to be accurate with current specs). The function expects from the Uri segment to the process (LocalPath when working with web requests). Example call 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 ); } 

Call example ..

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

This is a very late answer ... But since this was the first hit on google, I will post what I found!

You can simply force the compiler to name the embedded resource whatever you want; Which will solve this problem from the very beginning ... You only need to edit the csproj file, which you usually do if you want to use 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 tell Visual Studio that I want all the files in "some folder" to be imported as embedded resources. I also want them to appear under "some folder" in VS Solution Explorer (this is a link tag). And finally, when compiling them, I want them to be named with exactly the same name and address that they have on my disk, with the prefix "somefolder: \". The last part does the magic.

+2
source

Roel

Hmmm ... This is a hack, but I think it should work. Just define an empty Marker class in each directory that contains resources, then get the FullName of this type, remove the class name from the end and wala: there is your decoded path.

string path = (new MarkerClass()).GetType().FullName.Replace(".MarkerClass", "");

I'm sure there is a โ€œbetterโ€ way to do this ... with lots of lines of code; and this has the advantage that Microsoft supports it when they change material; -)

Greetings. Whale.

+1
source

The late answer here also, I googled before I tried this on my own, and I eventually had to.

In this 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); } 
0
source

All Articles