本帖最后由 愤怒的小小鸟 于 2013-11-6 21:32 编辑
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace 文件缓存管理器
{
class Program
{
static void Main(string[] args)
{
string s=FileCacheManager.Instance.IsExist(@"c:\1.txt");
Console.WriteLine(s);
Console.Read();
}
}
public sealed class FileCacheManager
{
public static readonly FileCacheManager Instance = new FileCacheManager();
//将dic作为缓存:存储实际文件路径,文件内容
private Dictionary<string, CacheItem> dic = new Dictionary<string, CacheItem>();
public string IsExist(string filepath)
{
if (dic.ContainsKey(filepath))
{
//返回内容
return dic[filepath].Content;
}
else
{
//将文件内容存入缓存
string s = File.ReadAllText(filepath);
CacheItem item = new CacheItem();
//item.Content = s;
//dic[filepath]=item;
dic[filepath].Content = s;//这段报错,说给定关键字不在字典中,但是改为上面两行代码就行,为什么?
return s;
}
}
}
class CacheItem
{
public string Content { get; set; }
public DateTime LastWriteTime { get; set; }
}
}
|