コード例 #1
0
       public FilesStorage(string name, string path, string ext, double iExpires)
       {
           if (path[path.Length-1]=='/')
           {
               path.Substring(0, path.Length - 1);//去除最后的/
           }
           path = System.Web.HttpContext.Current.Server.MapPath(path);
           fileName = path = "/" + name + ext;
           FileInfo f;
           bool exists = false;
           bool timeout = true;
           if (File.Exists(fileName))
           {
               exists = true;
               f = new FileInfo(fileName);
               if (f.LastWriteTime>DateTime.Now)
               {
                   timeout = false;
               }
           }
           else
           {
               f = null;
           }
           if (exists && timeout) //如果过期并存在
           {
               try
               {
                   f.Delete();
               }
               catch 
               {
               }
           }

           if (File.Exists(fileName)) 
           {
               store.Timeout = DateTime.Now.AddSeconds(iExpires);//更新时间
               String Data = Xml.Encode(store);
               using (FileStream fs = new FileStream(fileName, FileMode.Create))
               {
                   using (StreamWriter sw = new StreamWriter(fs))
                   {
                       sw.WriteLine(Data);
                   }
               }
               f = new FileInfo(fileName);
           }

           f.LastWriteTime = DateTime.Now.AddSeconds(iExpires);//设置新的过期时间
           String AllData = "";
           using (StreamReader sr = new StreamReader(fileName))
           {
               AllData = sr.ReadToEnd();
           }
           try
           {
               store = Xml.Decode(AllData, store.GetType()) as FileStore;
           }
           catch
           {
               store = new FileStore();
           }
       }