[Assembly].GetExecutingAssembly().GetName().Name Missing Underscor

G

Guest

I am attempting to embed a text resource in my assembly and following MSDN
article
http://msdn.microsoft.com/library/d...mactiontocreatedatabaseduringinstallation.asp
using reflection to access the full path of the embedded resource. The
assembly name is MyCompany Database Translator which is correctly reported
using [Assembly].GetExecutingAssembly().GetName().Name. Here is the code
supplied to get access to the embedded resource:

Private Function GetSql(ByVal Name As String) As String
Try

' Gets the current assembly.
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()

' Resources are named using a fully qualified name.
Dim strm As Stream = Asm.GetManifestResourceStream(Asm.GetName().Name
+ "." + Name)

' Reads the contents of the embedded file.
Dim reader As StreamReader = New StreamReader(strm)
Return reader.ReadToEnd()
Catch ex As Exception
MsgBox("In GetSQL: " & ex.Message)
Throw ex
End Try

End Function

Using the method above fails because it tried to load MyCompany Database
Translator.textfile.txt, the resource is actually stored at
MyCompany_Database_Translator.textfile.txt with underscores in place of the
spaces. Is there another property I can access to get the correctly formatted
assembly path or is a string.replace the only way to address this? Thank you.
 
M

Mattias Sjögren

Is there another property I can access to get the correctly formatted
assembly path or is a string.replace the only way to address this? Thank you.

Why do you have to name the resource the same as the assembly name?
Can't you just give the text file a name without spaces?



Mattias
 
G

Guest

Mattias Sjögren said:
Why do you have to name the resource the same as the assembly name?
Can't you just give the text file a name without spaces?

The project, executable and assembly name are the same automatically. The
Project name is MyCompany Database Translator, the executable is MyCompany
Database Translator.exe and therefore, the assembly is MyCompany Database
Translator. Any resource I embed should be at the path MyComany Database
Translator.resource.ext but it instead uses the default namespace for the
path of MyCompany_Database_Translator.resource.ext. There are no spaces in
the name of my resource file, just the actual project and executable itself.
The question therefore being, how do I use reflection to access the correct
root path name for my assembly instead of using
GetExecutingAssembly.GetName.Name.String.Replace(" "c,"_"c). Thanks.
 
M

Mattias Sjögren

The project, executable and assembly name are the same automatically.

Unless you change them.

Any resource I embed should be at the path MyComany Database
Translator.resource.ext but it instead uses the default namespace for the
path of MyCompany_Database_Translator.resource.ext. There are no spaces in
the name of my resource file, just the actual project and executable itself.

Right, it's the default namespace that matters, not the assembly name.
Since namespaces can't contain spaces they are replaced by
underscores.

The question therefore being, how do I use reflection to access the correct
root path name for my assembly instead of using
GetExecutingAssembly.GetName.Name.String.Replace(" "c,"_"c). Thanks.

Change the root namespace to contain periods instead of underscores
(MyCompany.Database.Translator for example). Then just get the
namespace of a class at the root level, for example using
GetType(YourMainClass).Namespace.

Or even better, remove the root namespace so that the resource name
becomes simply "resource.txt".



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top