程式如下:
public static string ReadFile(string path)
{
if (!File.Exists(path))
{
Console.WriteLine("{0} do not exist",path.Substring(path.LastIndexOf(@"\")+1));
return string.Empty;
}
String text = File.ReadAllText(path,Encoding.UTF8);
Console.WriteLine("文件读取完成:"+text);
return text;
}
读取一个61k的全国城镇json文件就很久,大概用2-3秒这样子,很影响窗口的打开,
每次打开窗口都要等.这样怎么处理呢?
就这个函数,绝对不可能用2-3秒。
可能是其他部分的原因。
在程序里多加几个时间戳,排查一下。
为什么不用异步调用呢,这样ui线程不会被阻塞,建议用async await方式处理
只读取了这么一个文件么?按理说不会啊,是不是有杀毒软件什么的干扰。文件系统的故障或者什么问题。
这个函数没有问题,时间长可能是其他地方引起的,把其它地方的代码也贴出来
使用文件流的读取方式
用StreamReader读取快
public void test(string path)
{
System.IO.StreamReader reader = new System.IO.StreamReader(path);
string text = reader.ReadToEnd();
reader.Close();
}
StreamReader读取就行。这么小的文件会卡死这应该不是读取的问题,单步检查一下
http://www.51aspx.com/code/codename/58803 看看这个我写的,多线程 异步处理 200M txt的数据分析