| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using ControlzEx.Standard;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- using TeamAAS_VP;
- using TeamAAS_VP.Models;
- using TeamAAS_VP.Resources.Languages;
- using static Org.BouncyCastle.Math.EC.ECCurve;
- namespace TeamAAS_VP.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(string.Format(Lang.文件不存在0, 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(string.Format(Lang.文件不存在0, path));
- }
- }
- /// <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;
- }
- }
- }
- }
|