public static string LoadFile(string path) { if (!File.Exists(path)) return null; FileStream fs = null; for (int i = 0; i < 5; i++)//尝试打开5次 { try { fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); break; } catch { Thread.Sleep(50);//暂停50毫秒 } } if (fs == null) throw new IOException("Unable to open the file: " + path); StreamReader sr = new StreamReader(fs, Encoding.Default); string res = sr.ReadToEnd(); sr.Close(); fs.Close(); return res; }
{
if (!File.Exists(path)) return null;
FileStream fs = null;
for (int i = 0; i < 5; i++)//尝试打开5次
{
try
{
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
break;
}
catch
{
Thread.Sleep(50);//暂停50毫秒
}
}
if (fs == null) throw new IOException("Unable to open the file: " + path);
StreamReader sr = new StreamReader(fs, Encoding.Default);
string res = sr.ReadToEnd();
sr.Close();
fs.Close();
return res;
}
但是这么做就是有点浪费资源。每次都要COPY。。期待高手解决