using HighWayIot.Common.XmlConfig; using HighWayIot.Log4net; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HighWayIot.Common.LANConnectConfig { /// /// 局域网连接配置类 /// public class LANConnect { private static readonly Lazy lazy = new Lazy(() => new LANConnect()); public static LANConnect Instance { get { return lazy.Value; } } private static LogHelper _logHelper = LogHelper.Instance; XmlUtil xmlConfig = XmlUtil.Instance; /// /// 读取网络文件夹中的txt文件 /// /// 路径 /// txt名称 /// public string ReadTxt(string path, string txtName) { string sourceFile = Path.Combine(path, txtName); try { if (File.Exists(sourceFile)) { string content = File.ReadAllText(sourceFile); _logHelper.Info("读取到内容:" + content); return content; } else { _logHelper.Error("源文件不存在: " + sourceFile); return string.Empty; } } catch (Exception ex) { _logHelper.Error("读取文件发生错误", ex); return string.Empty; } } /// /// 向网络文件夹内写入txt文件 /// /// 写入路径 /// txt名称 /// 写入内容 /// public bool WriteTxt(string path, string txtName, string content) { string newFile = Path.Combine(path, txtName); try { File.WriteAllText(newFile, content); _logHelper.Info("已写入文件:" + newFile); return true; } catch (Exception ex) { _logHelper.Error("写入文件发生错误", ex); return false; } } /// /// 移动文件到一个新位置 /// /// 源路径 /// 目标路径 /// 文件名称 /// public bool MoveFile(string sourcePath, string targetPath, string fileName) { // 3. 剪切(移动)文件到另一个网络文件夹 string moveFileSource = Path.Combine(sourcePath, fileName); string moveFileTarget = Path.Combine(targetPath, fileName); try { // 先创建一个待移动的文件 if (File.Exists(moveFileSource)) { if (File.Exists(moveFileTarget)) { File.Delete(moveFileTarget); // 目标存在就先删除,避免冲突 } File.Move(moveFileSource, moveFileTarget); _logHelper.Info("已移动文件到:" + moveFileTarget); return true; } else { _logHelper.Error("源文件不存在"); return false; } } catch (Exception ex) { _logHelper.Error("移动文件发生错误", ex); return false; } } } }