2

I have some BIML that generates SSIS packages based on data in some config files.

I have C# code in the BIML to read the config files and use the data within them when building the SSIS packages.

However, I currently have to hard code the path to the config files in my C#. I would prefer to use a relative path from the BIML files. Is this possible? (Or is there a simpler way?)

Minimal example with everything in one BIML file (in reality I have separate C# files):

<Biml xmlns="http://schemas.varigence.com/biml.xsd">

<#
// I would like a directory path to any of my BIML or C# files here.
var CodeBase = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
var BaseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
var CurrentDirectory = Environment.CurrentDirectory;
var GetCurrentDirectory = System.IO.Directory.GetCurrentDirectory();
var GetFullPath = System.IO.Path.GetFullPath(".");
#>

    <Packages>
        <Package Name="Demo_paths" ConstraintMode="Linear">
            <Variables>
                <Variable Name="CodeBase" DataType="String" IncludeInDebugDump="Exclude" EvaluateAsExpression="false">
                  <#=CodeBase#>
                </Variable>
                <Variable Name="BaseDirectory" DataType="String" IncludeInDebugDump="Exclude" EvaluateAsExpression="false">
                  <#=BaseDirectory#>
                </Variable>
                <Variable Name="CurrentDirectory" DataType="String" IncludeInDebugDump="Exclude" EvaluateAsExpression="false">
                  <#=CurrentDirectory#>
                </Variable>
                <Variable Name="GetCurrentDirectory" DataType="String" IncludeInDebugDump="Exclude" EvaluateAsExpression="false">
                  <#=GetCurrentDirectory#>
                </Variable>
                <Variable Name="GetFullPath" DataType="String" IncludeInDebugDump="Exclude" EvaluateAsExpression="false">
                  <#=GetFullPath#>
                </Variable>
            </Variables>
        </Package>
    </Packages>
</Biml>

I've also tried RootNode.BimlFile but that doesn't work in the minimal repro, and only appears to give the file name of the file, not its path on disk.

The variables in the above have the following values

BaseDirectory:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\

CodeBase:

file:///C:/Users/<USERNAME>/AppData/Local/Temp/Varigence/230028/4pxczy3m.dll

CurrentDirectory:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE

GetCurrentDirectory:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE

GetFullPath:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE

Dan
  • 7,446
  • 6
  • 32
  • 46
  • Where are you trying to retrieve this directory and what was the result of the attempts you have mentioned in your question? – iamdave Aug 17 '18 at 10:37
  • You may also find your answer here: https://stackoverflow.com/questions/816566/how-do-you-get-the-current-project-directory-from-c-sharp-code-when-creating-a-c – iamdave Aug 17 '18 at 10:38
  • The issue appears to be that BIML compiles the C# to a temporary assembly and then runs it with a current directory of the Visual Studio install directory, so I don't think any C# based solution will work without support from BIML – Dan Aug 20 '18 at 12:52

1 Answers1

3

So, one method that works in BimlExpress is to use

Host.TemplateFile

Although this does appear to depend on the particular way that BIML generates its output and so might break in the future.

Dan
  • 7,446
  • 6
  • 32
  • 46