Stefan Steiger
Stefan Steiger

Reputation: 82186

Embedded ressource in VB.NET vs. C#

Question:

I have embedded ressources (SQL scripts) in my application, which get used by my DAL dll, which i use in my application.

Now the embedded ressources reside in folder SQL/SQL_Server/ and SQL/MS_Access/.

I read them out at runtime, using the server type (Access, SqlServer) and the filename.

This works fine, because the embedded ressourcename is: myappname.Folder.Subfolder.FileName, e.g. appname.SQL.MS_Access.Filename

However, if I now call this dll from VB, the ressourcename only is: myappname.FileName

Is there a way to get the folder name of the embedded ressource in VB.NET ?
Or to include it when compiling ?
Or to distinguish in the DAL between a VB.NET host and a C# host ?

System.Reflection.Assembly asmDataSourceAssembly = System.Reflection.Assembly.GetEntryAssembly();

string strRessourceRoot = null;
foreach (string strThisRessourceName in asmDataSourceAssembly.GetManifestResourceNames())

Then compare with strThisRessourceName.ToLower().EndsWith("sql.foldername.filename")

To be more exact:
Main.exe in C# with embedded ressources + DAL.DLL in C# = works
Main.exe in VB.NET with embedded ressources + DAL.DLL in C# = not working because GetManifestResourceNames() only gets the filename of the embedded ressources, and not the folder names as well. As an additional side effect, one cannot embed several files with the same name in a VB.NET exe, even when they are in a different folder (compile time error). (the same thing works absolutely fine in C#).

Upvotes: 2

Views: 774

Answers (2)

Erik A. Brandstadmoen
Erik A. Brandstadmoen

Reputation: 10588

There is actually a difference on how C# and VB.NET handles this, but It is the language of the DLL that the embedded resource exists in that decides the namespace. We've had problems with the paths changing after we've converted some old VB.NET code into C# with automated tools.

More here: http://weblogs.asp.net/istofix/archive/2008/10/21/embedded-resources-in-vb-net-and-c-projects.aspx

Upvotes: 1

Rowland Shaw
Rowland Shaw

Reputation: 38130

The VB compiler does not implicitly map subfolders to be sub-namespaces, therefore, to achieve the effect of sub namespacing below the default namespace for the assembly (as configured through project properties, defaults to the project name), you will need to include the sub namespaces in the file name

Upvotes: 2

Related Questions