using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace TEAMModelOS.Helper.Common.FileHelper
{
    public static class FileTool
    {


        public static string getJson(string contentRootPath, string name)
        {
            //string webRootPath = _hostingEnvironment.WebRootPath;

            try
            {
                String path = contentRootPath + "/JsonFile/" + name + ".json";
                //获取正在占用的文件
                FileStream fs = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
                String line;
                StringBuilder builder = new StringBuilder();
                while ((line = sr.ReadLine()) != null)
                {
                    builder.Append(line.ToString());
                }

                sr.Close();
                string log = builder.ToString();
                return log;
            }
            catch (Exception e)
            {
                var s = e.Message;
                return s;
            }
        }

        public static string UnicodeToString(string unicode)
        {
            string result = "";
            //正则匹配
            Regex reg = new Regex(@"(?i)//u([0-9a-f]{4})");
            result = reg.Replace(unicode, delegate (Match m)
            {
                return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
            });
            return result;
        }
    }
}
