none
How to access text file in VSTO SE Excel Addin directory

    Question

  • I am creating an Excel Addin with VSTO SE.  I want to add custom menu items to the Worksheet Menu Bar using the information stored in an external file stored in the same directory as the app.  1) How do I get a file in this folder?  2) How do I access the folder?  I have tried using My.Application.Info.DirectoryPath.  This gives me the folder, but the file I added to the bin for the app doesn't get copied there.

    How can I accomplish this, or is there another way.  I don't want to hard code the menu items as there will be many and they will change with time.

    Monday, March 24, 2008 6:37 PM

Answers

  • This kind of question more involves the .NET Framework than the VSTO technology. For questions such as this it's often better to ask in a forum that specializes in the .NET Framework. For example, you can make your file an "embedded Resource", then Setup should pick it up and copy it automatically.

    See this message thread, for example

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2976048&SiteID=1

     

    Here's a code snippet that reads the content of an XML embedded as a resource in the project named WordDocx_VB. Note the use of GetExecutingAssembly to obtain the location of the files (the Bin folder).

     

    Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly

    Dim ioStream As System.IO.Stream = asm.GetManifestResourceStream("WordDocx_VB.Test.xml")

    Dim ioReader As System.IO.StreamReader = New System.IO.StreamReader(ioStream)

    MessageBox.Show(ioReader.ReadToEnd())

    Tuesday, March 25, 2008 11:15 AM
    Moderator
  • Hello dbKemp,

     

    I agree to Cindy,

    personally I 'm using this snippet to get the correct Path to the AddIn installation directory.

    Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Substring(8)));

    Note that is only correct when the Path is a local path and starts with eg: file:///C:\

     

    An Addin could also be installed on a UNC Path.

    But this should give you a startingpoint.

    To get your fine into the bin directory, you should set the correct properties in your project.

    Build Action = Content

    Copy local = if newer or always - as you like.

     

    Hope this helps,

    greets, Helmut

     

     

     

    Tuesday, March 25, 2008 7:23 PM
    Answerer

All replies

  • This kind of question more involves the .NET Framework than the VSTO technology. For questions such as this it's often better to ask in a forum that specializes in the .NET Framework. For example, you can make your file an "embedded Resource", then Setup should pick it up and copy it automatically.

    See this message thread, for example

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2976048&SiteID=1

     

    Here's a code snippet that reads the content of an XML embedded as a resource in the project named WordDocx_VB. Note the use of GetExecutingAssembly to obtain the location of the files (the Bin folder).

     

    Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly

    Dim ioStream As System.IO.Stream = asm.GetManifestResourceStream("WordDocx_VB.Test.xml")

    Dim ioReader As System.IO.StreamReader = New System.IO.StreamReader(ioStream)

    MessageBox.Show(ioReader.ReadToEnd())

    Tuesday, March 25, 2008 11:15 AM
    Moderator
  • Hello dbKemp,

     

    I agree to Cindy,

    personally I 'm using this snippet to get the correct Path to the AddIn installation directory.

    Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase.Substring(8)));

    Note that is only correct when the Path is a local path and starts with eg: file:///C:\

     

    An Addin could also be installed on a UNC Path.

    But this should give you a startingpoint.

    To get your fine into the bin directory, you should set the correct properties in your project.

    Build Action = Content

    Copy local = if newer or always - as you like.

     

    Hope this helps,

    greets, Helmut

     

     

     

    Tuesday, March 25, 2008 7:23 PM
    Answerer