Does AppDomain.RelativeSearchPath replacement in .NET Core exist? - c#

I have following code to retrieve application binary directory in desktop .NET Framework:
static string GetBinDirectory()
{
var relativePath = AppDomain.CurrentDomain.RelativeSearchPath;
var basePath = AppDomain.CurrentDomain.BaseDirectory;
return string.IsNullOrEmpty(relativePath) || !relativePath.IsSubdirectoryOf(basePath)
? basePath
: relativePath;
}
This code works correct in corner cases like assembly shadow copying running NUnit tests.
I started migration this code to .NET Core platform.
I found that there is IHostingEnvironment.ContentRootPath property in ASP.NET Core.
Which is the way to get correct path to application binaries in pure .NET Core?
Which is the replacement for AppDomain.RelativeSearchPath property?

You are probably looking for PlatformServices.Default.Application.ApplicationBasePath which is the path to the folder the application lives in.

Related

how to implement Application.UserAppDataPath in .net standard 2.0

Currently in our Application is in .net 4.8 and we are using Application.UserAppDataPath to get the path for the application data of a user.
if (TestManager.Singleton.IsUnitTestActive)
{ // Unit test is running...
string appUserPath = Application.UserAppDataPath;
this.Path = appUserPath.Substring(0, appUserPath.LastIndexOf('\\')) + "\\Config.xml";
}
recently we have migrated the logic into .net Standard 2.0 to make it crossplatform.
is there any way to find the path in .net Standard

What is the right way to add app.config to a .net core wpf application since it doesn't come with it unlike .net framework?

I like setting my connection string in my app.config and this has been very easy for me for a .net framework. Since I migrated to .net core I noticed that it doesn't come with app.config.
In .NET Core and ASP.NET Core applications the application settings are stored in a JSON file named appsettings.json. This is the default configuration for .NET Core projects. INI and XML files is also supported but JSON is the default.
You may also added to .Net Core WPF project appsettings.json config file, for this you need to install following nuget packegs:
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.DependencyInjection
after this,
{
"ConnectionStrings": {
"myDataBaseConnectionString": "Server=myServer;Database=myDataBase;Trusted_Connection=True;"
}
}
and you can read it in your code by calling the GetConnectionString method in the Microsoft.Extensions.Configuration namespace.
var myDbConnectionString = _configuration.GetConnectionString("myDataBaseConnectionString");
Second way, you may use static class for keeping your database connection data:
public static class dbConfig
{
public const string ConnectionString =
"Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = TCP)(PROTOCOL = TCP)(HOST =192.100.10.11)(PORT = 1521)))(CONNECT_DATA = (SID = testdb))); User Id =admin; Password=admin;Pooling=true";
}
and you may use from it any place of your assambly

access outside project folder with Asp.net core application on linux

I'm having some issues trying to access a folder outside the application root folder with an asp.net application deployed on Linux.
The applcation is deployed at the moment (for testing purposes) in /home/pbl/projects/pbl-web/.
I want to access a folder of an external hard drive I've mounted on the system, and it's located in /mnt/ExtDist/Data.
I'm using a PhysicalFileProvider with the following path: /mnt/ExtDist/Data. This path is configured in app.settings.json file and retrieved through the IConfiguration configuration variable.
Here's a part of the code in the Startup.cs file :
public void ConfigureServices(IServiceCollection services)
{
...
var imagePath = configuration.GetSection("PhotoManagementSettings")["ImagesFolderPath"];
var rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
this.imagePhysicalFileProvider = new PhysicalFileProvider(imagePath));
I've tried in different ways with no luck so far:
passing the absolute path
passing the relative path and combining with the rootPath variable (see the code above).
The PhysicalFileProvider is getting me the following error:
Unhandled exception. System.IO.DirectoryNotFoundException: /mnt/ExtDist/Data/
Testing the code in windows and giving it an absolute path like i.e "C:\Test" works fine.
So there's something weird in linux that is failing, but I cannot understand why. Any clues ?
Thanks in advance
Paolo

Windows.ApplicationModel.Package not available in NUnit-Test in .NET 4.5 (Windows 8 App)

I want to create an NUnit test in an Windows 8 app (under .NET 4.5) where I read the contents of an XML file in my application to run some code on this XML structure.
In the NUnit test I want to do something like this:
var file = Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(#"testxml.xml");
// settings
var _Path = #"InternetZeitung\Tests\testxml.xml";
var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// acquire file
var _File = _Folder.GetFileAsync(_Path).GetResults();
Assert.IsNotNull(_File, "Acquire file");
// read content
var _ReadThis = Windows.Storage.FileIO.ReadTextAsync(_File);
The I would do something with this. The problem is that calling Windows.ApplicationModel.Package in any way raises an exception, because the unit-tested code isn't a package. Now how can I then read a file from the project using the .NET 4.5 APIs without using this (to my knowledge) only way to find out the path of the file?

Using C# How to detect if Windows Installer 4.5 is Installed

I am trying to figure out the most efficient way to determine if Windows Installer 4.5 is installed on a machine.
I have a 2.0 application (cannot convert at this time to 3.5) and we are upgrading from MSDE to SQL 2008 Express. One of the requirements of 2008 Express is that Windows Installer 4.5 is installed on the machine. This application is deployed globally to machines both on and off of an internal network.
I would prefer to run a batch file or C# code to determine the installer version.
Please let me know your recommended methods and provide some code (or links to code).
Thanks!
You can read the file version of the msi.dll library in the system directory:
using System.Diagnostics;
using System.IO;
public bool IsWindowsInstaller45Installed()
{
FileVersionInfo info;
string fileName = Path.Combine(Environment.SystemDirectory, "msi.dll");
try {
info = FileVersionInfo.GetVersionInfo(fileName);
} catch (FileNotFoundException) {
return false;
}
return (info.FileMajorPart > 4
|| info.FileMajorPart == 4 && info.FileMinorPart >= 5);
}
Check the version of the MSI.DLL file that's in your System32 directory.
You should be able to use GetFileVersionInfo or GetFileVersionInfoEx to get out the version number.
This MSDN article has some sample code: Unsafe Code Tutorial
Like Ho1 said, you can go by the version of MSI.dll in System32 but you don't need to P/Invoke you can use the FileVersionInfo class found in System.Diagnostics.

Categories

Resources