简体   繁体   中英

c# Getting other project directory within solution

So, I have a solution with projects for separation of concerns/logics... Database context, services, UI and so ON. What I want is to be able to get a folder location in my UI project from my database context project to be used when I update my SQLite via EF. I can easily get bin folder within my UI project and that technically works fine, because bin folder SQLite file will be used when project runs, but bin files are not included in source control, so having database file reside in outside the bin folder which on build gets copied to bin file is just a better approach.

 _connectionString = $"Data Source={AppContext.BaseDirectory}\\Sqlite\\SomeName\\SomeName.sqlite";

This will get the bin builder as base directory when EF merge is performed. I did try variety of ways to get it, but apart from hard coding it, I was unable to achieve what I want. Hard coding is obviously not an option. Is it possible to get solutions location and then use that to navigate to different projects in solution? Either .Net Core or .Net framework approach isfine.

To clarify, I want my data context project to create SQLite database file in another project folder and I want that file not to be within /bin folder that is generated on run. I need a way to dynamically get location of another project within a solution.

Say I have a solution that is made up out of 2 projects.

  1. Database context Project
  2. UI/api project

UI/API project has bunch of folders, as you would expect.I want to get a location of one of them to use in my connection string. And no web config is not suitable as those are not dynamic by any means, second they are getting deprecated and for good reason, but using json configuration file is not an option either, as its still not dynamic. Config files works for database that is always in same place, and your project location doesnt matter. But in this case if someone else pulled my source code it would not be in same directory and SQLite files are local and not hosted online.

What I am trying to get is path to my solution would it be c:/projects/Solution or d:/c#/random/StackOverflower/Projects/Solution

What I can get is c:/projects/Solution/UI/Bin/Debug

My end goes is to get c:/projects/Solution/UI/Sqlite/

I think you are looking for msbuild tasks in your project file(copy task in particular).

https://msdn.microsoft.com/en-us/library/3e54c37h.aspx

I wrote a blog post about it some time ago. I have stopped blogging so the code part went missing for some reason so i put it here.

https://torontoprogrammers.blogspot.com/2014/11/msbuild-targets-and-tasks.html

<copy destinationfolder="..\References\Libraries\" overwritereadonlyfiles="true" sourcefiles="$(OutputPath)\$(AssemblyName).dll" />   
 <copy destinationfolder="..\References\Libraries\" overwritereadonlyfiles="true" sourcefiles="$(OutputPath)\$(AssemblyName).pdb" />    
 <copy destinationfolder="..\References\Libraries\" overwritereadonlyfiles="true" sourcefiles="$(OutputPath)\$(AssemblyName).xml" />  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM