123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- using LampInspectionMachine.Model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace LampInspectionMachine.Core
- {
- public static class FileHelper
- {
- /// <summary>
- /// 写入Json文件
- /// </summary>
- /// <param name="obj">对象</param>
- /// <param name="path">文件路径</param>
- public static void WriteJsonFile(object obj, string path)
- {
- if ( !Directory.Exists(Path.GetDirectoryName(path)) )
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- }
- using ( StreamWriter sw = new StreamWriter(path) )
- {
- string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
- sw.Write(JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented));
- }
- }
- /// <summary>
- /// 读取Json文件
- /// </summary>
- /// <typeparam name="T">对象</typeparam>
- /// <param name="path">文件路径</param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static T ReadJsonFile<T>(string path)
- {
- if ( File.Exists(path) )
- {
- string buffer;
- using ( StreamReader sr = new StreamReader(path) )
- {
- buffer = sr.ReadToEnd();
- }
- return JsonConvert.DeserializeObject<T>(buffer);
- }
- else
- {
- throw new Exception($"文件不存在:[{path}]");
- }
- }
- /// <summary>
- /// 写入文件
- /// </summary>
- /// <param name="content">对象</param>
- /// <param name="path">文件路径</param>
- public static void WriteFile(string content, string path)
- {
- if ( !Directory.Exists(Path.GetDirectoryName(path)) )
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- }
- using ( StreamWriter sw = new StreamWriter(path) )
- {
- sw.Write(content);
- }
- }
- /// <summary>
- /// 读取文件
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static string ReadFile(string path)
- {
- if ( File.Exists(path) )
- {
- string buffer;
- using ( StreamReader sr = new StreamReader(path) )
- {
- buffer = sr.ReadToEnd();
- }
- return buffer;
- }
- else
- {
- throw new Exception($"文件不存在:[{path}]");
- }
- }
- /// <summary>
- /// 读取软件设置参数
- /// </summary>
- /// <returns></returns>
- public static List<CameraInfo> ReadApplicationConfiguration()
- {
- try
- {
- if ( File.Exists(FilePath.ApplicationConfigurationPath) )
- {
- var config = FileHelper.ReadJsonFile<List<CameraInfo>>(FilePath.ApplicationConfigurationPath);
- return config;
- }
- else
- {
- var config= new List<CameraInfo>();
- FileHelper.WriteJsonFile(config, FilePath.ApplicationConfigurationPath);
- return config;
- }
- }
- catch ( Exception )
- {
- return new List<CameraInfo>();
- }
- }
- /// <summary>
- /// 保存软件设置参数
- /// </summary>
- /// <param name="config"></param>
- public static void SaveApplicationConfiguration(List<CameraInfo> config)
- {
- try
- {
- //config.DT = DateTime.Now;
- FileHelper.WriteJsonFile(config, FilePath.ApplicationConfigurationPath);
- }
- catch ( Exception )
- {
- }
- }
- /// <summary>
- /// 检测文件名是否合规
- /// </summary>
- /// <param name="filename"></param>
- /// <returns></returns>
- public static bool CheckFileName(string filename)
- {
- //
- // 定义文件名合法性的正则表达式
- string pattern = @"^[^-\\/:*?""<>|\x00-\x1F]*$"; // 匹配不包含非法字符的文件名
- // 使用正则表达式进行匹配
- if ( Regex.IsMatch(filename, pattern) )
- {
- //Console.WriteLine("输入的名称符合文件名命名规则,并且不包含非法字符。");
- return true;
- }
- else
- {
- //Console.WriteLine("输入的名称不符合文件名命名规则,或者包含非法字符。");
- return false;
- }
- }
- }
- }
|