建湖县建设局网站,食品包装设计特点,青岛人社通app下载最新版,网页设计需要什么技术项目中需要读取一个文本文件的内容#xff0c;调用C#的File.ReadAllLines(path)方法#xff0c;但是报错#xff0c;就提示unknown exception#xff0c;也没其他提示了。
文件是在的#xff0c;并且#xff0c;如果把文件拷贝到另外一个路径#xff0c;再次读取是正常…项目中需要读取一个文本文件的内容调用C#的File.ReadAllLines(path)方法但是报错就提示unknown exception也没其他提示了。
文件是在的并且如果把文件拷贝到另外一个路径再次读取是正常的。
仔细研究了一下应该是客户电脑上跑了其他程序正在往这个文件里写东西把文件lock了导致ReadAllLines()方法读不了。
网上查到了解决办法
public static string[] readAllLines(String i_FileNameAndPath)
{string[] o_Lines null;Liststring list new Liststring();int i 0;using (FileStream fileStream File.Open(i_FileNameAndPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){using (StreamReader streamReader new StreamReader(fileStream)){while (streamReader.Peek() -1){string line streamReader.ReadLine(); list.Add(line);i;}}}o_Lines list.ToArray();return o_Lines;
}
参考C# – Can’t read all lines in file that being used by another process – iTecNote