How can I get to the root folder of another project in a .NET Core solution? - asp.net-core

I need to read the settings file (appsettings.json) from another project in my solution. When I use:
Directory.GetCurrentDirectory()
From within the current project, I get the following path:
{projectRootFolder}\bin\Debug\netcoreapp3.0\
My question is: How can I get to the exact same folder in another project in the same solution? Or is there a better way to access the settings file from another project within the current solution?

If I understand the problem correctly there are two misconceptions:
It has little sense to access output directory of an another project as the structure has sense in compile time only. You will not have the same structure in run-time once the application is "published".
The Directory.GetCurrentDirectory() returns the current working directory. It is just a coincidence to be set to project output directory by Visual Studio. It can be totally different directory.
It is not clear to me what exactly you are trying to achieve. I recommend using the configuration system provided by .net core to access the configuration and add that other appsettings.json as another configuration provider.
If you really need to open the settings file then the project with the settings file (A) should mark the file as "Copy to Output Directory" and the project to open the file (B) should reference the project A. So the settings file will be copied to output of the project A too.

What you're attempting to do is not possible. There's no inherent way for ASP.NET Core to know where a totally different app running in a totally different process is located.
If you need to access appsettings.json from another project, then you would need to include it as a linked file in your project, and set it to copy to output. Then, you're accessing it actually from your project (which is all you can do), but the file itself is shared.
However, this is almost always a bad idea, and usually a sign that you're doing something wrong. If you truly do need to share the settings, then what you should be doing is putting them in a distributed config provider like Azure Key Vault or similar, where both projects can independently access the settings from a common store.

Related

Deploy problem with project with an access database integrated

I´ve been having problems with my project and the database it has. I have created a simple project and I created an access database in it, the database is in the project folder. This is the connection string specified in the App.config:
<connectionStrings>
<add name="Project.My.MySettings.BaseDeDatosConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Recursos\Datos\BaseDeDatos.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>
But when I publish my project, install it in my computer and executed it I get the error:
'C:\Users\USER_NAME\AppData\Local\Apps\2.0\Data\6QPXHXGQ.ZM0\MVT91HXD.V1B\simu..tion_0000000000000000_0001.0000_49edcdec2714f7aa\Data\Resources\Data\Database.accdb' is not a valid path . Make sure that the path is spelled correctly and that you are connected to the server where the file is located.
And of course the program remains useless. I´ve checked the folder and indeed there is no data base in there, I didn't even knew it would create a file there (I know nothing related to databases and deployment, I am just following tutorials). I was using a SQL data base before and it worked fine after installation, but I had to change it. What could I be missing?
I am using VS 2015 in spanish and VB.NET.
If you are using the Publish functionality in VS then you are using ClickOnce. ClickOnce apps have a dedicated folder to which "|DataDirectory|" resolves at run time. It is NOT the program folder. If you want to use "|DataDirectory|" in your connection string then you have to specify that the data file is indeed a data file via the properties, so that it will be placed in that folder. I don't really use ClickOnce but I believe that, to do that, you need to open the Publish page of the project properties, click the Application Files button and then set the Publish Status of that file to Data File. I'm not sure whether the subfolders for the source file will be honoured after publishing or not but you can test that.
Note that, if you simply copy the application files to the target machine or use some installer technology other than ClickOnce then "|DataDirectory|" will resolve to the program folder. For the record, it resolves to the App_Data folder for ASP.NET apps (possibly only Web Forms, not sure).

In VStudio 2019, VB, .NET-5: for a ClickOnce installed app, how do I locate the application's data directory at runtime?

My VStudio is 2019 Community, the application is an old VB Windows/Forms app being brought forward from .Net-3.5-ish to .Net-5.
Under .Net-4.8 or earlier, using the namespace System.Deployment.Application, one could access ApplicationDeployment.DataDirectory to locate that artificially-generated directory in \Users\Username\AppData\Local\Apps\2.0\... that a 1-click installation would create and populate with data files from the Project Build/Publish.
Under .Net-5, I believe that particular set of functions has been taken out - yet the ClickOnce installer still places Data files there. How do I programmatically locate that directory? The Application Path can be located from System.AppContext.BaseDirectory, but that doesn't help in locating the data.
This app, an old bit of VB code, has a large number of application data files that need to be installed along with the code. As there are more than 40 of them, we've kept them in their own \Data directory, which is included in the Project, is under Git control to keep their versions aligned with their code...
In an uninstalled app, we could keep this \Data directory adjacent to the executable, and find it easily at runtime: but with a ClickOnce installed app, that falls apart. In the Publish configuration, all the data files are identified, with Group=Required, Publish Status=DataFile.
Can I either:
Easily configure the 1-click installer to place our \Data directory and its contents into one of the standard \Local\Appname or \Roaming\Appname directories that seem to also get created, but left empty? Those can easily be located at runtime.
Easily construct at runtime the path to that obscure data directory that 1-click normally creates and uses for data?
Cheers, and Thanks! Bob

Which files are used by a program?

I have written a program on Visual Basic. In the debug folder, there are many files:
Database1.mdf
Database1_log.ldf
MyData.Designer.vb
MyData.xsc
MyData.xsd
MyData.xss
WindowsApplication1.exe
WindowsApplication1.config
WindowsApplication1.pdb
WindowsApplication1.vshost
WindowsApplication1.vshost.exe
WindowsApplication1.vshost.exe.manifest
WindowsApplication1.xml
I want to publish my program. Are all of those files necessary for the program? Which of them are used for my database?
Because I want to put a button in my program that backs up the database. Which files must be backed up?
First of all, you should publish the Release version of your software, not the debug version so the files will be a bit different. As for which files to publish, if you use the Setup project you will be able to select the files based upon what your application needs. For example, it looks like you are including database files with your application (Database1.mdf and Database1_log.ldf). You could add these files to the setup project.
The setup project will know to include your exe and your config file (unless you tell it not to) so you will be covered there. Here is a video and a written walkthrough of how to create a Setup project:
http://msdn.microsoft.com/en-us/library/ms241903.aspx
http://www.youtube.com/watch?v=Lcue0jo41AM
As for your PDB files, these are the Program Database Files that are used for debugging (and should never be give to the customer/end user).
http://msdn.microsoft.com/en-us/library/ms241903.aspx
As for backing up your database, back up the MDF and LDF files.
No, all of the files above are from your debug compile output. You can change what is output by changing your build configuration. Go to Build, Configuration Manager and switch to Release. It's also on the toolbar.
In general your ProjectName.exe (but not the .vshost.exe), .config (but not the .vshost.exe.config) and MDF/LDF files are needed for publishing. You also have an XSD File which will also be needed.
The MDF/LDF files are your database.

Setting up StyleCop for team development

We are trying to setup stylecop for a team development environment. So far what we have done is:
Checked the files into source control
Create an environment variable on every machine that points to that location (each dev has source checked out to a different location, this solves that)
Add the tag to the project as follows:
This works great, but VS complains that the file is unsafe, and I know to fix that we have to mark is safe in the registry. We wanted to create a .reg file to import this setting and make it easier for everyone. Can we use that environment variable in the path? I have tried the snippet below, but that doesn't seem to work. Is the syntax for an environment variable different?
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\9.0\MSBuild\SafeImports]
"StyleCop.4.3"="%StyleCopLocation%\\Microsoft.StyleCop.Targets"
Why you need to host that Targets file in a global place? Everyone can install a copy of StyleCop.
If you in fact plan to share StyleCop settings, please configure the projects to use a project locally setting file (*.SourceAnalysis). You can check in this file along with your projects, and then everyone is in sync.

With nHibernate, how can I manage my hibernate.cfg.xml file in both my web app and test project?

Seeing as the hibernate.cfg.xml file has to be in the running application (in this case a web application), what is the best way to have the file in both my test project and my web app project?
You can add the file to multiple projects. If you use visual studio you can right click one of the projects and than add existing item. Click on the arrow on the right of the add button. Click on "add as link" The same file is added to both the projects now.
I don't recommend to use the same database for automatic tests, because the tests will run slow and it will be difficult to ensure the state of the database before each test.
I'd suggest that you place the xml file into a seperate directory and link/share the file. You can refer to DimeCasts.net for more information. www.dimecasts.net/Casts/CastDetails/10
The easiest way is to just put a copy of the XML file in both projects. You'll just have to be careful to keep all the configuration settings in synch in both places. This will allow for easy debugging within the test project or web app. If you have a build system setup, which creates deployments for testing, you could store the xml file in a common location, then create a build action to copy it to the output directory of both projects when you go to deploy/test.