【发布时间】:2019-08-24 23:51:04
【问题描述】:
这是一个简单的代码,但一直无法读取 Excel 文件。我之前使用过 using 语句,认为 mayeb 在读取前使用 dispose 但仍然是同样的问题。
ReadTimeout = 'file.ReadTimeout' 引发了“System.InvalidOperationException”类型的异常
WriteTimeout = 'file.WriteTimeout' 引发了“System.InvalidOperationException”类型的异常
string filePath = @"C:\Users\test\source\repos\MnMC\MnMC\wwwroot\CFolder\PBot\Files\Incidents\PBot_Incident.xlsx";
var builder = new BodyBuilder { HtmlBody = "Incident Report." };
if (System.IO.File.Exists(filePath))
{
try
{
MemoryStream memoryStream = new MemoryStream();
FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
file.CopyTo(memoryStream);
builder.Attachments.Add("PBot_Incident.xlsx", memoryStream);
}
catch (Exception ex)
{
throw;
}
}
【问题讨论】:
-
授予对您的 exe 文件的读写权限或以管理员身份运行它或授予管理员对您的 excel 文件的读写权限。
-
那些“错误”只是调试器通知,MemoryStream 没有实现超时。您必须专注于编写正确的代码,插入 memoryStream.Position = 0;在 CopyTo() 调用之后。并且您必须使用 using 语句才能正确关闭文件。
-
谢谢汉斯,我一使用 position = 0,它就起作用了。
标签: c#