C# 如何对同样在Excel中打开的文件执行File.ReadAllLines?
如何将同样在Excel中打开的文本文件的所有行读取到C# 如何对同样在Excel中打开的文件执行File.ReadAllLines?,c#,io,C#,Io,如何将同样在Excel中打开的文本文件的所有行读取到string[]中,而不获取IO异常 有一个问题可能是答案的一部分,尽管我不知道如何使用其中的内容: 试试这个。 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, System.IO.FileShare.ReadWrite) 当以读取限制打开文件时,不能仅以读取方式打开该文件。否则,所有方法(包括ReadAllLines)都将在不引发权限异常的情况
string[]
中,而不获取IO异常
有一个问题可能是答案的一部分,尽管我不知道如何使用其中的内容:
试试这个。FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read,
System.IO.FileShare.ReadWrite)
当以读取限制打开文件时,不能仅以读取方式打开该文件。否则,所有方法(包括
ReadAllLines
)都将在不引发权限异常的情况下工作。您的问题是Excel以读/写方式打开文件<代码>文件。ReadAllLines()当文件打开以在其他应用程序中写入时,无法访问该文件。如果在Excel中以只读方式打开csv,则不会遇到此异常
这是因为当另一个应用程序具有文件写入权限时,.Net中的实现没有打开具有适当权限的内部流来访问该文件
因此,这里的修复很简单,编写自己的ReadAllLines()
方法,在启动底层流时设置适当的权限
这里有一个想法大量借鉴了ReadAllLines()
本身所做的工作:
public string[] WriteSafeReadAllLines(String path)
{
using (var csv = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var sr = new StreamReader(csv))
{
List<string> file = new List<string>();
while (!sr.EndOfStream)
{
file.Add(sr.ReadLine());
}
return file.ToArray();
}
}
要了解StreamReader
内部正在做什么:
internal StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool checkHost)
{
if (path == null || encoding == null)
throw new ArgumentNullException(path == null ? "path" : "encoding");
if (path.Length == 0)
throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
this.Init((Stream) new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan, Path.GetFileName(path), false, false, checkHost), encoding, detectEncodingFromByteOrderMarks, bufferSize, false);
}
internal StreamReader(字符串路径、编码、bool detectencodingfromteordermarks、int bufferSize、bool checkHost)
{
if(路径==null | |编码==null)
抛出新ArgumentNullException(路径==null?“路径”:“编码”);
if(path.Length==0)
抛出新的ArgumentException(Environment.GetResourceString(“Argument_EmptyPath”);
如果(bufferSize)您对链接到的答案有什么不理解的地方?这很清楚。您尝试过吗?向我们展示您的代码。如何使用该代码填充我的字符串[]有文本文件行的行
?这里没有任何内容。@m0s仍然无法回答如何将所有行读入字符串array@user1306322您可以使用StreamReader的ReadLine方法获取单行并添加到字符串列表中。完成后,将列表转换为数组。问题的直接答案是您不能使用文件.ReadAllLines在锁定的文件上。如果您仍然不知道如何使用streamreader将文件读入数组,请将其作为单独的问题提问。最后,您可以尝试制作锁定文件的临时副本,然后在副本上尝试file.ReadAllLines。我该如何做string[]lines=something.ReadAllLines(myPath)
这样做?我不确定我是否需要一个文件流来读取文本文件中的行。还有其他方法吗?这很有意义,我相信它现在可以工作。@user1306322它对我有效。我只是在读取excel中打开的CSV时遇到了同样的问题,找到了你的问题,但不满意答案,我就是这样解决的。因此,如果您有任何问题(除了我提到的问题外),请让我知道。可能是Stack Overflow@psubsee2003的最佳答案太棒了!我从一个保持打开的日志文件中读取行时遇到问题。这解决了问题。升级。
internal StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool checkHost)
{
if (path == null || encoding == null)
throw new ArgumentNullException(path == null ? "path" : "encoding");
if (path.Length == 0)
throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));
if (bufferSize <= 0)
throw new ArgumentOutOfRangeException("bufferSize", Environment.GetResourceString("ArgumentOutOfRange_NeedPosNum"));
this.Init((Stream) new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan, Path.GetFileName(path), false, false, checkHost), encoding, detectEncodingFromByteOrderMarks, bufferSize, false);
}