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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Shank

    #1

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

    I am attempting to embed a text resource in my assembly and following MSDN
    article
    http://msdn.microsoft.com/library/de...stallation.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].GetExecutingAs sembly().GetNam e().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].GetExecutingAs sembly()

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

    ' Reads the contents of the embedded file.
    Dim reader As StreamReader = New StreamReader(st rm)
    Return reader.ReadToEn d()
    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.text file.txt, the resource is actually stored at
    MyCompany_Datab ase_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.
  • Mattias Sjögren

    #2
    Re: [Assembly].GetExecutingAs sembly().GetNam e().Name Missing Underscor

    [color=blue]
    >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.[/color]

    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

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Jim Shank

      #3
      Re: [Assembly].GetExecutingAs sembly().GetNam e().Name Missing Under

      "Mattias Sjögren" wrote:
      [color=blue]
      >[color=green]
      > >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.[/color]
      >
      > 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?
      >
      >[/color]

      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.reso urce.ext but it instead uses the default namespace for the
      path of MyCompany_Datab ase_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
      GetExecutingAss embly.GetName.N ame.String.Repl ace(" "c,"_"c). Thanks.


      Comment

      • Mattias Sjögren

        #4
        Re: [Assembly].GetExecutingAs sembly().GetNam e().Name Missing Under

        [color=blue]
        >The project, executable and assembly name are the same automatically.[/color]

        Unless you change them.

        [color=blue]
        >Any resource I embed should be at the path MyComany Database
        >Translator.res ource.ext but it instead uses the default namespace for the
        >path of MyCompany_Datab ase_Translator. resource.ext. There are no spaces in
        >the name of my resource file, just the actual project and executable itself.[/color]

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

        [color=blue]
        >The question therefore being, how do I use reflection to access the correct
        >root path name for my assembly instead of using
        >GetExecutingAs sembly.GetName. Name.String.Rep lace(" "c,"_"c). Thanks.[/color]

        Change the root namespace to contain periods instead of underscores
        (MyCompany.Data base.Translator for example). Then just get the
        namespace of a class at the root level, for example using
        GetType(YourMai nClass).Namespa ce.

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



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        Working...