using Newtonsoft.Json;
|
using Newtonsoft.Json.Linq;
|
using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using System.Linq;
|
|
namespace Bro.Common.Helper
|
{
|
public class SettingHelper
|
{
|
const string SETTINGFILE = "Setting.json";
|
const string PROPERTY_PRODUCTIONCODES = "ProductionCodes";
|
const string PROPERTY_CONFIGPATH = "ConfigPath";
|
const string PROPERTY_PROCESSCODES = "ProcessCodes";
|
const string PROPERTY_DESCRIPTION = "Description";
|
const string PROPERTY_ICONPATH = "IconPath";
|
|
static JObject data = null;
|
static JObject Data
|
{
|
get
|
{
|
string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SETTINGFILE);
|
if (File.Exists(configPath))
|
{
|
using (StreamReader reader = new StreamReader(configPath, System.Text.Encoding.UTF8))
|
{
|
string dataStr = reader.ReadToEnd();
|
data = JsonConvert.DeserializeObject<JObject>(dataStr);
|
}
|
}
|
|
return data;
|
}
|
}
|
|
public static List<string> GetProductionCodes()
|
{
|
List<string> codes = new List<string>();
|
|
if (Data != null && Data.ContainsKey(PROPERTY_PRODUCTIONCODES))
|
{
|
codes = Data.Value<JArray>(PROPERTY_PRODUCTIONCODES).ToList().ConvertAll(u => u.ToString());
|
}
|
|
if (codes.Count == 0)
|
{
|
codes.Add("Default");
|
}
|
|
return codes;
|
}
|
|
public static List<string> GetProcessCodes()
|
{
|
List<string> codes = new List<string>();
|
|
if (Data != null && Data.ContainsKey(PROPERTY_PROCESSCODES))
|
{
|
codes = Data.Value<JArray>(PROPERTY_PROCESSCODES).ToList().ConvertAll(u => u.ToString());
|
}
|
return codes;
|
}
|
|
public static List<string> AddNewProductionCode(string code)
|
{
|
List<string> codes = GetProcessCodes();
|
if (!codes.Contains(code))
|
{
|
codes.Add(code);
|
}
|
if (Data != null && Data.ContainsKey(PROPERTY_PRODUCTIONCODES))
|
{
|
Data[PROPERTY_PRODUCTIONCODES] = new JArray(codes);
|
string newDataStr = JsonConvert.SerializeObject(Data, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto });
|
using (StreamWriter writer = new StreamWriter(GetSettingFilePath(), false, System.Text.Encoding.UTF8))
|
{
|
writer.Write(newDataStr);
|
writer.Flush();
|
writer.Close();
|
}
|
}
|
|
return GetProcessCodes();
|
}
|
|
public static string GetConfigFilePath()
|
{
|
string path = "";
|
|
if (Data != null && Data.ContainsKey(PROPERTY_CONFIGPATH))
|
{
|
path = Data.Value<string>(PROPERTY_CONFIGPATH);
|
}
|
|
return path;
|
}
|
|
public static string GetSettingFilePath()
|
{
|
string path = "";
|
|
if (Data != null && Data.ContainsKey(SETTINGFILE))
|
{
|
path = Data.Value<string>(SETTINGFILE);
|
}
|
|
return path;
|
}
|
|
public static string GetProgramDescription()
|
{
|
string desc = "伯肯森自动化技术有限公司";
|
|
if (Data != null && Data.ContainsKey(PROPERTY_DESCRIPTION))
|
{
|
desc = Data.Value<string>(PROPERTY_DESCRIPTION);
|
}
|
|
return desc;
|
}
|
|
public static string GetProgramIcon()
|
{
|
string iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logo.ico");
|
|
if (!File.Exists(iconPath))
|
{
|
iconPath = "";
|
}
|
|
if (Data != null && Data.ContainsKey(PROPERTY_ICONPATH))
|
{
|
iconPath = Data.Value<string>(PROPERTY_ICONPATH);
|
}
|
|
return iconPath;
|
}
|
}
|
}
|