C#Blazor初始化时读取配置

2022/11/12 program 共 937 字,约 3 分钟

Program.cs里调用:

var builder = WebApplication.CreateBuilder(args);

// 添加这句
Settings.initEnv(builder.Configuration);

builder.Services.AddRazorPages();
public static void initEnv(IConfiguration config) {
    // 解析工具路径
    ApkInfoTool = config["xxTool"];
    if (!File.Exists(xxTool)) { throw new Exception("file not found: " + xxTool); }


    // api接口服务器配置
    apiServerIp = config["apiServerIp"];
    apiServerPort = Convert.ToInt32(config["apiServerPort"]);

    // 文件服务器配置
    saveFileServerIp = config["saveFileServerIp"];
    saveFileServerPort = Convert.ToInt32(config["saveFileServerPort"]);

    saveFileRootDir = config["saveFileRootDir"];
    saveAppRelvPath = config["saveAppRelvPath"];
    saveImgRelvPath = config["saveImgRelvPath"];

    if (!Directory.Exists(saveFileRootDir)) { throw new Exception("dir not found: " + saveFileRootDir); }

    saveAppFullPath = Path.Combine(saveFileRootDir, saveAppRelvPath);
    saveImgFullPath = Path.Combine(saveFileRootDir, saveImgRelvPath);
    Directory.CreateDirectory(saveAppFullPath);
    Directory.CreateDirectory(saveImgFullPath);
}

文档信息

Search

    Table of Contents