软件雏形
This commit is contained in:
83
AppConfig.cs
Normal file
83
AppConfig.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace CopyRou
|
||||
{
|
||||
public class AppConfig
|
||||
{
|
||||
public List<string> SourcePaths { get; set; } = new();
|
||||
public string DestPath { get; set; } = string.Empty;
|
||||
|
||||
private static readonly string ConfigPath = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
||||
"CopyRou", "config.json");
|
||||
|
||||
public static AppConfig Load()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(ConfigPath))
|
||||
{
|
||||
var json = File.ReadAllText(ConfigPath);
|
||||
return JsonSerializer.Deserialize<AppConfig>(json) ?? new AppConfig();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果加载失败,返回默认配置
|
||||
System.Diagnostics.Debug.WriteLine($"加载配置失败: {ex.Message}");
|
||||
}
|
||||
|
||||
return new AppConfig
|
||||
{
|
||||
SourcePaths = new List<string>
|
||||
{
|
||||
@"Z:\Routing\A1-ROUTING\NEW-ROUTING",
|
||||
@"Z:\Routing\A2-ROUTING\NEW-ROUTING"
|
||||
},
|
||||
DestPath = @"D:\ARPTWork\rou"
|
||||
};
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
try
|
||||
{
|
||||
var directory = Path.GetDirectoryName(ConfigPath);
|
||||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||||
Directory.CreateDirectory(directory);
|
||||
|
||||
var json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });
|
||||
File.WriteAllText(ConfigPath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
System.Diagnostics.Debug.WriteLine($"保存配置失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public string GetSourcePathsText()
|
||||
{
|
||||
return string.Join(Environment.NewLine, SourcePaths);
|
||||
}
|
||||
|
||||
public void SetSourcePathsFromText(string text)
|
||||
{
|
||||
SourcePaths.Clear();
|
||||
if (!string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
var paths = text.Split(new[] { Environment.NewLine, "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var path in paths)
|
||||
{
|
||||
var trimmedPath = path.Trim();
|
||||
if (!string.IsNullOrEmpty(trimmedPath))
|
||||
{
|
||||
SourcePaths.Add(trimmedPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user