222 lines
7.8 KiB
C#
222 lines
7.8 KiB
C#
using IRaCIS.Core.Domain.Share;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration.Json;
|
|
using IRaCIS.Core.Infrastructure.Extention;
|
|
using SharpCompress.Common;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Text.Encodings.Web;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace IRaCIS.Core.Application.Service.Common
|
|
{
|
|
[ApiExplorerSettings(GroupName = "Common")]
|
|
public class SystemMonitor : BaseService
|
|
{
|
|
|
|
public void GetDiskInfo()
|
|
{
|
|
// Get all drives
|
|
var drives = DriveInfo.GetDrives();
|
|
|
|
// Loop through each drive and get info
|
|
foreach (var drive in drives)
|
|
{
|
|
if (drive.IsReady && drive.DriveType == DriveType.Fixed)
|
|
{
|
|
long totalSize = drive.TotalSize;
|
|
long availableSpace = drive.AvailableFreeSpace;
|
|
long usedSpace = totalSize - availableSpace;
|
|
double usedSpacePercent = (double)usedSpace / totalSize * 100;
|
|
|
|
Console.WriteLine($"Drive {drive.Name}: Total size = {totalSize / (1024 * 1024)} MB, Available space = {availableSpace / (1024 * 1024)} MB, Used space = {usedSpace / (1024 * 1024)} MB ({usedSpacePercent:F2}%)");
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public string GetBestStoreDisk()
|
|
{
|
|
|
|
//默认存储的路径
|
|
var defaultStoreRootFolder = Path.Combine((Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\'))).IfNullThrowException().FullName, StaticData.Folder.IRaCISDataFolder);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var drives = DriveInfo.GetDrives().Where(t => !t.Name.Contains("C") && !t.Name.Contains("c"))
|
|
.Where(d => d.DriveType == DriveType.Fixed && d.IsReady)
|
|
.OrderByDescending(d => d.AvailableFreeSpace)
|
|
.ThenByDescending(d => d.TotalSize - d.TotalFreeSpace);
|
|
|
|
var bestDrive = drives.FirstOrDefault();
|
|
var bestStoreRootFolder = string.Empty;
|
|
|
|
//仅仅只有C 盘
|
|
if (bestDrive == null)
|
|
{
|
|
bestStoreRootFolder = defaultStoreRootFolder;
|
|
|
|
}
|
|
else
|
|
{
|
|
bestStoreRootFolder = drives.FirstOrDefault()?.Name + _hostEnvironment.EnvironmentName;
|
|
}
|
|
|
|
DriveInfo drive = new DriveInfo(Path.GetPathRoot(defaultStoreRootFolder));
|
|
|
|
//最优盘符使用率超过百分之80
|
|
if (((double)(drive.TotalSize - drive.TotalFreeSpace) / drive.TotalSize) * 100 > 80)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
return bestStoreRootFolder;
|
|
|
|
}
|
|
|
|
|
|
public void UpdateAppSettings(string key, string newValue)
|
|
{
|
|
|
|
#region NewtonSoftJson
|
|
//// 读取 JSON 文件
|
|
//string jsonFilePath = "appsettings.json";
|
|
//string jsonString = File.ReadAllText(jsonFilePath);
|
|
|
|
//// 解析 JSON 文件
|
|
//var jObject = JsonConvert.DeserializeObject<JObject>(jsonString);
|
|
|
|
//// 更新或添加配置项
|
|
//jObject[key] = newValue;
|
|
|
|
//// 将更新后的 JSON 字符串写入文件
|
|
//using var streamWriter = new StreamWriter(jsonFilePath);
|
|
//using var jsonTextWriter = new JsonTextWriter(streamWriter);
|
|
//jsonTextWriter.Formatting = Formatting.Indented;
|
|
//jObject.WriteTo(jsonTextWriter);
|
|
#endregion
|
|
|
|
#region test
|
|
//var json = File.ReadAllText("appsettings.json");
|
|
//var jObject = JToken.Parse(json, new JsonLoadSettings { CommentHandling = CommentHandling.Load,DuplicatePropertyNameHandling=DuplicatePropertyNameHandling.Ignore });
|
|
|
|
//// Update or add the property
|
|
//jObject[key] = newValue;
|
|
|
|
//// Preserve comments
|
|
//var settings = new JsonSerializerSettings
|
|
//{
|
|
// PreserveReferencesHandling = PreserveReferencesHandling.Objects,
|
|
// Formatting = Formatting.Indented,
|
|
// StringEscapeHandling = StringEscapeHandling.Default,
|
|
|
|
//};
|
|
//var updatedJson = JsonConvert.SerializeObject(jObject, settings);
|
|
//File.WriteAllText("appsettings.json", updatedJson);
|
|
|
|
|
|
|
|
//var json = File.ReadAllText("appsettings.json");
|
|
//var jToken = JToken.Parse(json);
|
|
|
|
//// Update or add the property
|
|
//jToken[key] = newValue;
|
|
|
|
//// Preserve comments
|
|
//var settings = new JsonSerializerSettings
|
|
//{
|
|
// TypeNameHandling = TypeNameHandling.All,
|
|
// PreserveReferencesHandling = PreserveReferencesHandling.Objects,
|
|
// Formatting = Formatting.Indented,
|
|
// StringEscapeHandling = StringEscapeHandling.Default
|
|
//};
|
|
//var updatedJson = JsonConvert.SerializeObject(jToken, settings);
|
|
//File.WriteAllText("appsettings.json", updatedJson);
|
|
|
|
|
|
//string json = @"{
|
|
// // This is a comment
|
|
// ""Name"": ""John Smith"",
|
|
// ""Age"": 30,
|
|
// ""Address"": {
|
|
// // This is another comment
|
|
// ""Street"": ""123 Main St"",
|
|
// ""City"": ""Anytown"",
|
|
// ""State"": ""CA"",
|
|
// ""Zip"": ""12345""
|
|
// }
|
|
//}";
|
|
|
|
//// 使用 Json.NET 库解析 JSON 字符串
|
|
//JObject jObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load });
|
|
|
|
//// 修改属性
|
|
//jObject["Name"] = "Jane Smith";
|
|
|
|
//// 将修改后的 JObject 对象序列化为 JSON 字符串并保留注释
|
|
//string newJson = jObject.ToString(Newtonsoft.Json.Formatting.Indented);
|
|
|
|
//// 打印修改后的 JSON 字符串
|
|
//Console.WriteLine(newJson);
|
|
|
|
#endregion
|
|
|
|
|
|
// 读取 Json 文件
|
|
string jsonFilePath = "appsettings.json";
|
|
|
|
var json = File.ReadAllText("appsettings.json");
|
|
|
|
JObject jsonObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load });
|
|
// 获取 UpdateConfig 属性所在的节点
|
|
JToken updateConfigNode = jsonObject.SelectToken("UpdateConfig");
|
|
|
|
JProperty updateConfigProperty = (JProperty)updateConfigNode.Parent;
|
|
JObject updateConfigParent = (JObject)updateConfigProperty.Value;
|
|
|
|
// 向 updateConfigParent 添加或者属性
|
|
updateConfigParent[key] = newValue;
|
|
|
|
|
|
// 将更改保存回 Json 文件
|
|
File.WriteAllText(jsonFilePath, jsonObject.ToString());
|
|
|
|
//using (var reader = new JsonTextReader(new StringReader(json)))
|
|
//using (var writer = new JsonTextWriter(new StreamWriter("appsettings.json")))
|
|
//{
|
|
// while (reader.Read())
|
|
// {
|
|
// if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == key)
|
|
// {
|
|
// // Write the updated property value
|
|
// writer.WritePropertyName(key);
|
|
// writer.WriteValue(newValue);
|
|
// reader.Read();
|
|
// continue;
|
|
// }
|
|
|
|
// // Write all other tokens
|
|
// writer.WriteToken(reader.TokenType, reader.Value);
|
|
// }
|
|
//}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|
|
}
|