1

My Excel VSTO Workbook's set debug path is C:\Users\MyName\Desktop\Testing\Excel Stuff\VSTO\Debug\

I am attempting to add an appsettings.json file to unity

private void ThisWorkbook_Startup(object sender, System.EventArgs e)
{
    IUnityContainer container = new UnityContainer();

    var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .Build();

    MessageBox.Show(Directory.GetCurrentDirectory()); // Trying to see what the directory is here

    container.RegisterInstance<IConfiguration>(config);

    // Logging
    container.RegisterType<ILogWriter, TextLogger>();
    container.RegisterType<ILoggingService, LoggingService>();

    // Set up Context
    container.RegisterInstance<FinanceDWEntities>(new FinanceDWEntities());
    container.Resolve<MainService>();
}

When I have the MessageBox show the current directory, however, it only displays: C:\Users\MyName\Desktop, which would explain why my appsettings information is coming back null, however why is GetCurrentDirectory not returning the full Debug path?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
jDave1984
  • 896
  • 4
  • 13
  • 43
  • Is this really `Unity3d` specific or rather a general `c#` question? If this is really for Unity then you should in general probably rather use [`Application.persistentDataPath`](https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html). – derHugo Oct 30 '19 at 10:20

2 Answers2

0

current directory is different from original directory (which is what you want.) You can try AppDomain.CurrentDomain.BaseDirectory. This is the directory the assembler probes for assemblies (bin in most cases.)

alternatively, since you are running an app on a desktop:

System.IO.Path.GetDirectory(Application.ExecutablePath)

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.executablepath?view=netframework-4.8

terrencep
  • 675
  • 5
  • 16
0

see here: https://stackoverflow.com/a/6041505/559144 you should use

AppDomain.CurrentDomain.BaseDirectory 
Davide Piras
  • 43,984
  • 10
  • 98
  • 147