我有一些.csv文件,我正在使用它作为测试台的一部分.我可以打开它们并且没有任何问题地阅读它们,除非我已经在Excel中打开了文件,在这种情况下我获得了IOException:
System.IO.IOException:进程无法访问文件'TestData.csv',因为它正由另一个进程使用.
这是测试平台的片段:
using (CsvReader csv = new CsvReader(new StreamReader(new FileStream(fullFilePath, FileMode.Open, FileAccess.Read)), false))
{
// Process the file
}
Run Code Online (Sandbox Code Playgroud)
这是StreamReader的限制吗?我可以在其他应用程序(例如Notepad ++)中打开该文件,因此它不能成为操作系统问题.也许我需要使用其他一些课程?如果有人知道如何绕过这个(除了关闭excel!)我会非常感激.
我在C#中构建一个应用程序,我必须打开一个CSV文件来从中读取数据.当我尝试从C#打开CSV文件时,我在Excel中打开该文件时出现异常.例外情况表明该进程无法访问该文件,因为该文件已经打开.即使在其他应用程序中打开文件,如何解决此问题并打开文件?
谢谢,拉克什.
我正在使用C#framework 4.5,netoffice 1.6和sharpdevelop 4.4.1来操作位于网络共享中的excel工作簿,从Outlook中.
在某些时候,我需要更改工作簿对象(ewb)的文件访问权限,以便像这样读写:
ewb.ChangeFileAccess(Excel.Enums.XlFileAccess.xlReadWrite, System.Reflection.Missing.Value, true);
Run Code Online (Sandbox Code Playgroud)
在更改文件访问权限之前,我检查文件是否在服务器上被锁定.如果文件被锁定,我将通知用户稍后重试该操作.
现在,我想在通知中包含锁定excel文件的用户名.我搜索了msdn,netoffice论坛等等,但还没有找到解决方案.我知道,如果你打开excel文件readwrite,它会将用户的名字存储在xlsx文件中.如何通过c#访问该特定信息?
编辑:我最终这样做:
public string GetExcelFileOwner(string path, NetOffice.ExcelApi.Enums.XlFileFormat ffmt) {
string tempmark = "~$";
if(ffmt==NetOffice.ExcelApi.Enums.XlFileFormat.xlExcel8) {
tempmark = "";
}
string uspath = Path.Combine(Path.GetDirectoryName(path), tempmark + Path.GetFileName(path));
if (!File.Exists(uspath)) return "";
var sharing = FileShare.ReadWrite | FileShare.Delete;
using (var fs = new FileStream(uspath, FileMode.Open, FileAccess.Read, sharing))
using (var br = new BinaryReader(fs, Encoding.Default)) {
if(ffmt==NetOffice.ExcelApi.Enums.XlFileFormat.xlExcel8) {
byte[] ByteBuffer = new byte[500];
br.BaseStream.Seek(150, SeekOrigin.Begin);
br.Read(ByteBuffer, 0, 500);
return matchRegex(System.Text.Encoding.UTF8.GetString(ByteBuffer), @"(?=\w\w\w)([\w, ]+)").Trim();
}
else …Run Code Online (Sandbox Code Playgroud)