|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 局域网连接配置类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class LANConnect
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private static readonly Lazy<LANConnect> lazy = new Lazy<LANConnect>(() => new LANConnect());
|
|
|
|
|
|
|
|
|
|
public static LANConnect Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return lazy.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static LogHelper _logHelper = LogHelper.Instance;
|
|
|
|
|
|
|
|
|
|
XmlUtil xmlConfig = XmlUtil.Instance;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读取网络文件夹中的txt文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">路径</param>
|
|
|
|
|
/// <param name="txtName">txt名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 向网络文件夹内写入txt文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">写入路径</param>
|
|
|
|
|
/// <param name="txtName">txt名称</param>
|
|
|
|
|
/// <param name="content">写入内容</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 移动文件到一个新位置
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sourcePath">源路径</param>
|
|
|
|
|
/// <param name="targetPath">目标路径</param>
|
|
|
|
|
/// <param name="fileName">文件名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|