跨平台是否有其他替代代码

时间:2020-09-08 11:14:04

标签: asp.net asp.net-core .net-core asp.net-core-3.1

我基于Windows的项目,现在我将其转换为Linux和Mc。我想为跨平台更改此代码,以便它可以在所有平台上工作。

using Microsoft.Win32;

public class Program
    {
        private const string DefaultInstallPath = @"C.....";
        private const string ProductRegistryKey = @"......";
        private static string _appPath;
        //....
}

public static string AppPath
        {
            get
            {
                if (string.IsNullOrEmpty(_appPath))
                {
                    var currentDirectory = Directory.GetCurrentDirectory();

                    if (Debugger.IsAttached)
                    {
                        _appPath = currentDirectory;
                    }
                    else
                    {
                        _appPath = (string)Registry.GetValue(ProductRegistryKey, "InstallPath", DefaultInstallPath);
                    }

                    if (_appPath == null)
                    {
                        _appPath = DefaultInstallPath;
                    }

                    if (!Directory.Exists(_appPath))
                    {
                        _appPath = currentDirectory;
                    }
                }
                return _appPath;            
            }
        }

我想要适用于所有平台的代码。那我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

在asp.net核心上创建一个应用程序。

创建了用于多平台开发的核心框架。

我目前不知道您使用的是哪种框架,但我假设您正在使用.net框架。

从Visual Studio创建应用程序,然后选择基于.net核心框架的应用程序。 MVC,控制台等。

基本上,您在Windows上执行的所有编码理论上都将在linux上工作,因为linux的框架版本可以解释您在Windows中执行的编码。

当前所有的多平台开发都是通过C#内核完成的。

希望是有帮助的