Get the Windows system directory from the .NET Standard Library

Sounds like a simple question, but Google doesn't seem to have an answer for me.

In the .NET Framework, we can use it Environment.SystemDirectoryto get a system catalog (which will look something like C:\WINDOWS\System32), but this property does not exist in current versions of .NET Standard or .NET Core. Is there a way to get this folder in the .NET Standard library (or otherwise, .NET Core)?

For my purposes are not required to call returns something useful on non-Windows-platforms (and that he would return anyway? /lib? /usr/lib? Etc.), though, I think it would be great if done .

Now it seems that my best option is to try using C:\WINDOWS\System32directly, and if this does not exist, try using it C:\WinNT\System32, but it looks like such a hack to do it this way.

+4
source share
1 answer

.NET Core 2.0 (and .Net Standard 2.0) will containEnvironment.SystemDirectory , but in current versions 1.x not.

Although the property still exists as internal, so one way would be to access it using reflection if you want to rely on non-public code:

using System;
using System.Reflection;
using static System.Reflection.BindingFlags;


var systemDirectory = typeof(Environment).GetProperty("SystemDirectory", NonPublic | Static)
    .GetValue(null, null);

Windows 10 .Net Core SDK 1.0, .Net Core 1.1.1, C:\WINDOWS\system32. Ubuntu 16.04 NullReferenceException.

+5

Source: https://habr.com/ru/post/1671952/


All Articles