遇到的問題:1、緩存到本地的文件寫入一次,不能讀取,不能二次寫入。程序報異常:文件被占用
解決辦法:
FileStream fs = new FileStream(appPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
設置文件FileShare的模式和FileAccess模式,可以解決這個問題
原始代碼為: FileStream fs = new FileStream(appPath, FileMode.Open);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8);
webservice 定時緩存文件 需要在網站中加入Gloal.asax文件,在 void Application_Start(object sender, EventArgs e) (應用程序啟動的時候就會執行)
中添加計時器,定時執行緩存方法
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Interval = 600000; // 30000 毫秒 = 30秒
timer1.Elapsed += new System.Timers.ElapsedEventHandler(notma.NotamMemory);//執行的方法
timer1.AutoReset = true;//是否重復執行
timer1.Enabled = true;
web 讀取appconfig的方法 string appPath = System.Web.Configuration.WebConfigurationManager.AppSettings["appPath"];
cs讀取和修改appconfig的方法
//獲取Configuration對象
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//根據Key讀取<add>元素的Value
string name = config.AppSettings.Settings["timer"].Value;
//寫入<add>元素的Value
config.AppSettings.Settings["timer"].Value = this.txt_time.Text.Trim();
config.Save(ConfigurationSaveMode.Modified);
//刷新,否則程序讀取的還是之前的值(可能已裝入內存)
System.Configuration.ConfigurationManager.RefreshSection("appSettings");
timer1.Start();