118 lines
4.0 KiB
C#
118 lines
4.0 KiB
C#
![]() |
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Configuration;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Web;
|
|||
|
using System.Web.Configuration;
|
|||
|
|
|||
|
namespace Epost.Common
|
|||
|
{
|
|||
|
public class ConfigurationOperator
|
|||
|
{
|
|||
|
|
|||
|
public bool UpdateAppSettings(string key, string value)
|
|||
|
{
|
|||
|
bool reuslt = false;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
// Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
|
|||
|
Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
|
|||
|
SetFileAccess(config.FilePath + "", false);
|
|||
|
ConfigurationSection sections = config.GetSection("appSettings");
|
|||
|
bool isSet = false;
|
|||
|
for (int i = 0; i < ((System.Configuration.AppSettingsSection)(sections)).Settings.Count; i++)
|
|||
|
{
|
|||
|
string itemkey = ((System.Configuration.AppSettingsSection)(sections)).Settings.AllKeys[i];
|
|||
|
if (itemkey == key)
|
|||
|
{
|
|||
|
((System.Configuration.AppSettingsSection)(sections)).Settings.Remove(key);
|
|||
|
((System.Configuration.AppSettingsSection)(sections)).Settings.Add(key, value);
|
|||
|
isSet = true;
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
if (!isSet)
|
|||
|
{
|
|||
|
((System.Configuration.AppSettingsSection)(sections)).Settings.Add(key, value);
|
|||
|
}
|
|||
|
|
|||
|
config.Save();
|
|||
|
ConfigurationManager.RefreshSection("appSettings");
|
|||
|
|
|||
|
SetFileAccess(config.FilePath + "", true);
|
|||
|
reuslt = true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
// LogNet.Log.WriteLog("UpdateAppSettings", ex);
|
|||
|
}
|
|||
|
|
|||
|
return reuslt;
|
|||
|
}
|
|||
|
|
|||
|
protected void SetFileAccess(string path, bool isReadOnly)
|
|||
|
{
|
|||
|
FileInfo fi = new FileInfo(path);
|
|||
|
if (fi.IsReadOnly != isReadOnly)
|
|||
|
fi.IsReadOnly = isReadOnly;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//检查文件,如果文件不存在则创建
|
|||
|
public void ExistsFile(string FilePath)
|
|||
|
{
|
|||
|
//if(!File.Exists(FilePath))
|
|||
|
//File.Create(FilePath);
|
|||
|
//以上写法会报错,详细解释请看下文.........
|
|||
|
if (!File.Exists(FilePath))
|
|||
|
{
|
|||
|
FileStream fs = File.Create(FilePath);
|
|||
|
fs.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
public string ReadTxt()
|
|||
|
{
|
|||
|
string path = ConfigurationManager.AppSettings["SetupMode"];
|
|||
|
ExistsFile(path);//检查文件是否存在
|
|||
|
//读取文件
|
|||
|
StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);
|
|||
|
try
|
|||
|
{
|
|||
|
string input = sr.ReadToEnd();
|
|||
|
sr.Close();
|
|||
|
|
|||
|
input = input.Replace("\r\n", "").Replace("\n", "");
|
|||
|
return input;
|
|||
|
}
|
|||
|
catch(Exception ex)
|
|||
|
{
|
|||
|
LogHelper.WriteLogInfo("读取txt异常"+ex.Message);
|
|||
|
return "";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool WriteTxt(string txt)
|
|||
|
{
|
|||
|
string path = ConfigurationManager.AppSettings["SetupMode"];
|
|||
|
ExistsFile(path);//检查文件是否存在
|
|||
|
//写入文本
|
|||
|
StreamWriter sr = new StreamWriter(path, false, System.Text.Encoding.Default);
|
|||
|
try
|
|||
|
{
|
|||
|
sr.Write(txt);
|
|||
|
sr.Close();
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
LogHelper.WriteLogInfo("写入txt异常" + ex.Message);
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|