首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WEBSERVICE定时刷数据解决方案

2013-09-05 
WEBSERVICE定时刷数据在.NET上发过帖子了(http://bbs.csdn.net/topics/390578477?page1#post-395470520)

WEBSERVICE定时刷数据
在.NET上发过帖子了(http://bbs.csdn.net/topics/390578477?page=1#post-395470520)
但是么有解决,看这个版块下的一个帖子和我的情况很像(http://bbs.csdn.net/topics/390573177?page=1#post-395443621),cao版主说的判断时间怎么实现啊,我试了很久

[解决办法]

引用:
private static string _filecontent = null;

private static string FileContent
{
    get {
        if (_filecontent == null)
            _filecontent = string.Join("\r\n", System.IO.File.ReadAllLines(@"C:\1.txt"));
        return _filecontent;
    }
}

usage:
你的类.FileContent();

如果你的文本文件会更新,那么还需要加一个时间判断。



private static string _filecontent = null;
private static DateTime _date = new DateTime(1900,1,1);

private static string FileContent
{
    get {
        if (_filecontent == null 
[解决办法]
 _date.AddMinutes(5) < DateTime.Now)
        {
            _filecontent = string.Join("\r\n", System.IO.File.ReadAllLines(@"C:\1.txt"));
            _date = DateTime.Now;
        }
        return _filecontent;
    }
}


//其实你可以在get; set;里面做很多有意思的事情的,上面是当请求YourClass.FileContent的时候,如果是第一次请求(_filecontent是null)或者距离上次请求已经超过了5分钟,那么就重新读一遍。
或者你也可以不用和DateTime.Now比,而是用FileInfo.LastWriteTime,那么就是当文件发生更新的时候重读

private static string _filecontent = null;
private static DateTime _date = new DateTime(1900,1,1);

private static string FileContent
{
    get {
        FileInfo fi = new FileInfo(@"C:\1.txt");


        if (_filecontent == null 
[解决办法]
 _date < fi.LastWriteTime)
        {
            _filecontent = string.Join("\r\n", System.IO.File.ReadAllLines(@"C:\1.txt"));
            _date = fi.LastWriteTime;
        }
        return _filecontent;
    }
}[/code]

热点排行