using System.Text; using Newtonsoft.Json; using TouchSocket.Core; using TouchSocket.Dmtp; using TouchSocket.Dmtp.FileTransfer; namespace Net; public static class NetHelper { private static string _iP = "127.0.0.1"; private static int _port = 7789; private static string _rootPath = "C:\\FileTranslate"; private static string _serverRootPath = "C:\\FileTranslate\\S"; private static string _serverSaveRootPath = $"{_serverRootPath}\\Save"; private static string _clientRootPath = "C:\\FileTranslate\\C"; private static string _clientSaveRootPath = $"{_clientRootPath}\\Save"; private static bool _isConnected; private static bool _isSaveServerIP; private static bool _isShowLog; private static bool _isServer; private static string _fileFolder = "C:\\FileTranslate\\"; private static string _fileName = $"{_fileFolder}ZNetConfig.txt"; #region 属性 public static string IP { get => _iP; set { if (_iP == value || string.IsNullOrEmpty(value)) return; _iP = value; UpdateFile(); _data.IP = value; } } public static int Port { get => _port; set { if (_port == value) return; _port = value; UpdateFile(); _data.Port = value; } } public static string RootPath { get => _rootPath; set { if (_rootPath == value || string.IsNullOrEmpty(value)) return; string old = _serverRootPath; _rootPath = value; Directory.Move(old, value); _data.RootPath = value; UpdateFile(); } } public static string ServerRootPath { get => _serverRootPath; set { if (_serverRootPath == value || string.IsNullOrEmpty(value)) return; string old = _serverRootPath; _serverRootPath = value; Directory.Move(old, value); _data.ServerRootPath = value; UpdateFile(); } } public static string ServerSaveRootPath { get => _serverSaveRootPath; set { if (_serverSaveRootPath == value || string.IsNullOrEmpty(value)) return; _serverSaveRootPath = value; Directory.Move(_serverSaveRootPath, value); _data.ServerSaveRootPath = value; UpdateFile(); } } public static string ClientRootPath { get => _clientRootPath; set { if (_clientRootPath == value || string.IsNullOrEmpty(value)) return; _clientRootPath = value; Directory.Move(_clientRootPath, value); _data.ClientRootPath = value; UpdateFile(); } } public static string ClientSaveRootPath { get => _clientSaveRootPath; set { if (_clientSaveRootPath == value || string.IsNullOrEmpty(value)) return; _clientSaveRootPath = value; Directory.Move(_clientSaveRootPath, value); _data.ClientSaveRootPath = value; UpdateFile(); } } public static bool IsConnected { get => _isConnected; set { if (_isConnected == value) return; _isConnected = value; UpdateFile(); } } public static bool IsSaveServerIP { get => _isSaveServerIP; set { if (_isSaveServerIP == value) return; _isSaveServerIP = value; _data.IsSaveServerIP = value; UpdateFile(); } } public static bool IsShowLog { get => _isShowLog; set { if (_isShowLog == value) return; _isShowLog = value; _data.IsShowLog = value; UpdateFile(); } } public static bool IsServer { get => _isServer; set { _isServer = value; UpdateFile(); } } #endregion public static Server Server = null; public static Client Client = null; public static void Init() { if (!File.Exists(_fileName)) File.Create(_fileName).Dispose(); DeserializeFile(); if (!Directory.Exists(_rootPath)) Directory.CreateDirectory(_rootPath); if (!Directory.Exists(_serverRootPath)) Directory.CreateDirectory(_serverRootPath); if (!Directory.Exists(_serverSaveRootPath)) Directory.CreateDirectory(_serverSaveRootPath); if (!Directory.Exists(_clientRootPath)) Directory.CreateDirectory(_clientRootPath); if (!Directory.Exists(_clientSaveRootPath)) Directory.CreateDirectory(_clientSaveRootPath); } class Data { public string IP; public int Port; public string RootPath; public string ServerRootPath; public string ServerSaveRootPath; public string ClientRootPath; public string ClientSaveRootPath; public bool IsSaveServerIP; public bool IsShowLog; } private static Data _data; private static void SerializeFile() { var serializeObject = JsonConvert.SerializeObject(_data); File.WriteAllText(_fileName, serializeObject); } private static void DeserializeFile() { string json = File.ReadAllText(_fileName); Data? data = JsonConvert.DeserializeObject(json); if (data == null) { _data = new Data(); _data.IP = _iP; _data.Port = _port; _data.RootPath = _rootPath; _data.ServerRootPath = _serverRootPath; _data.ServerSaveRootPath = _serverSaveRootPath; _data.ClientRootPath = _clientRootPath; _data.ClientSaveRootPath = _clientSaveRootPath; _data.IsSaveServerIP = _isSaveServerIP; _data.IsShowLog = _isShowLog; UpdateFile(); return; } _data = data; _iP = data.IP; _port = data.Port; _rootPath = data.RootPath; _serverRootPath = data.ServerRootPath; _serverSaveRootPath = data.ServerSaveRootPath; _clientRootPath = data.ClientRootPath; _clientSaveRootPath = data.ClientSaveRootPath; _isSaveServerIP = data.IsSaveServerIP; _isShowLog = data.IsShowLog; } private static void UpdateFile() { SerializeFile(); // StringBuilder sb = new StringBuilder(); // sb.AppendLine($"服务器地址和端口:{_iP}:{_port}"); // sb.AppendLine($"根目录:{_rootPath}"); // sb.AppendLine($"服务器根目录:{_serverRootPath}"); // sb.AppendLine($"服务器保存根目录:{_serverSaveRootPath}"); // sb.AppendLine($"客户端根目录:{_clientRootPath}"); // sb.AppendLine($"客户端保存根目录:{_clientSaveRootPath}"); // sb.AppendLine($"是否保存服务器IP:{_isSaveServerIP}"); // sb.AppendLine($"是否显示日志:{_isShowLog}"); } }