Select to view content in your preferred language

Add-In: embedded Files and use it's path

1748
4
11-15-2010 11:45 PM
KatrinWalz
Emerging Contributor
Hi!
I am trying to export some metadata.
For this I want to use following code:
xml.SaveAsFile(path_xslt, "", false, ref path);


path_xslt includes the path to the xslt-File. I want this File to be included in the Add-In.

I found the following in the concepts for Add-Ins:
You can create additional folders and files under the Install folder. This is useful in cases where you want to ship data as part of your add-in. If you're adding data through your Visual Studio project, set the Copy to Output Directory property to the Copy always setting as shown in the following screen shot:


So I set the Copy to Output Directory property to "Copy always" and the Build Action to "AddInContent".

I am not really sure whether this is right. :confused:

Which is the correct way to embed my file? And on which path can I access that file?
0 Kudos
4 Replies
KatrinWalz
Emerging Contributor
okay. I figured out to copy my file into the addin itself.
(Just works how I described it wouldn't, but a day later, it just did, what it was supposed to do).

But I still have problems to gain access to that file when my solution is running.

Does anyone have a clue?

Thanks a lot.
0 Kudos
JeffreyHamblin
Occasional Contributor
Hi Katrin,

I think I may have it working:

In C# 2008 Solution Explorer I added a folder named "Test" and then a text file within it named "Text.txt" with just some dummy text.

On the properties for "Test.txt" file I set "Copy Always", but I left the Build Action as "Content".

Then I used this code to access the file:

First, a static property to get the path to the Add-in DLL:
// From StackOverflow
static public string AssemblyDirectory
{
    get
    {
        string codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        return System.IO.Path.GetDirectoryName(path);
    }
} 


Then this code in OnClick() for a button add-in:
string fileName = AssemblyDirectory + "/Test/Test.txt";

StreamReader inFile = new StreamReader(new FileStream(fileName,
        FileMode.Open, FileAccess.Read));

string msg = "";

try
{
    while (inFile.Peek() != -1)
    {
        msg += inFile.ReadLine() + "\n";
    }
}
finally
{
    inFile.Close();
}

MessageBox.Show(msg);


It's not heavily tested or optimized, but it should get you started.

-Jeff
0 Kudos
FernandoRangel
Deactivated User
Saved my day, thanks.

Fernando
0 Kudos
LesleyBross
Deactivated User
For anyone seeking to do this in VB, this code (slightly different) works for me:

Dim codeBase As String = System.Reflection.Assembly.GetExecutingAssembly.CodeBase
Dim uriBuilder As UriBuilder = New UriBuilder(codeBase)
Dim path As String = Uri.UnescapeDataString(uriBuilder.Path)
Return System.IO.Path.GetDirectoryName(path)


I can then create the full path to my file by appending a backslash and my file name.
0 Kudos