所以我在尝试阅读文件时遇到此问题。
我从目录中获取文件名,然后循环遍历它们以将它们插入到数据库中。但我不断收到错误。我尝试Streamread文件时无法访问文件。
这是我的所有代码,这是唯一实际使用此目录的代码,因此没有其他进程可以锁定此文件。
我一直在这一行得到错误"使用(var reader = new StreamReader(localPath))"说这个"该过程无法访问文件'文件名'因为它正被另一个进程使用"
public async Task<ResponseBase> GetCsvAsync()
{
var response = new ResponseBase();
try
{
DirectoryInfo d = new DirectoryInfo(HostingEnvironment.MapPath($"~/App_Data/IosStats/ToProcess"));//Assuming Test is your Folder
//FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files
FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files
var filenames = new List<string>();
foreach (var file in Files)
{
filenames.Add(file.Name);
}
foreach (var filename in filenames)
{
var localPath = HostingEnvironment.MapPath($"~/App_Data/IosStats/ToProcess/{filename}");
localPath = localPath ?? Path.GetFileName(filename);
var outputFile = System.IO.File.OpenWrite(localPath);
using (var reader = new StreamReader(localPath))
//using (var reader = new StreamReader($"{d}/{file.Name}"))
{
var counter = 0;
var columnName = "";
while (!reader.EndOfStream)
{
counter = counter + 1;
var line = reader.ReadLine();
var values = line.Split(',');
var currentRow = new AppDownload();
var currentAppHistory = DbContext.AppDownloads.Where(x => x.Platform == AppPlatform.IOS).ToList();
if (counter == 5)
{
columnName = values[1];
}
if (counter <= 4 & columnName != "")
{
var csvDate = Convert.ToDateTime(values[0]);
var isAlreadyLogged = currentAppHistory.Where(x => x.CsvDate == csvDate).ToList();
var currentReplacementRow = new AppDownload();
if (isAlreadyLogged.Count == 0)
{
currentRow.CsvDate = Convert.ToDateTime(values[0]);
currentRow.PackageName = "Dialdirect Insurance";
currentRow.Platform = AppPlatform.IOS;
currentRow.InsertedTime = DateTimeOffset.Now;
switch (columnName)
{
case "Active Devices":
currentRow.ActiveDeviceInstalls = Convert.ToInt16(values[1]);
break;
case "Installations":
currentRow.DailyDeviceInstalls = Convert.ToInt16(values[1]);
break;
default:
columnName = "";
break;
}
DbContext.AppDownloads.Add(currentRow);
}
else
{
currentReplacementRow = isAlreadyLogged.FirstOrDefault(x => x.CsvDate == csvDate);
switch (columnName)
{
case "Active Devices":
currentReplacementRow.ActiveDeviceInstalls = Convert.ToInt16(values[1]);
break;
case "Installations":
currentReplacementRow.DailyDeviceInstalls = Convert.ToInt16(values[1]);
break;
default:
columnName = "";
break;
}
DbContext.AppDownloads.Add(currentReplacementRow);
}
await DbContext.SaveChangesAsync();
var sourceFile = localPath;
var destinationFile = HostingEnvironment.MapPath($"~/App_Data/IosStats/Processed/{filename}");
System.IO.File.Move(localPath, destinationFile);
}
}
}
}
response.Success = true;
return response;
}
catch (Exception ex)
{
ex.ToExceptionless().Submit();
response.Success = false;
return response;
throw;
}
}
我正在拔头发,因为它不想读取文件。任何帮助将不胜感激
答案 0 :(得分:2)
您没有关闭outputFile
信息流,也似乎根本没有使用它。
另一个问题是这一行:
System.IO.File.Move(localPath, destinationFile);
您正在通过StreamReader
将文件保持打开状态来移动文件。
答案 1 :(得分:0)
如果甚至在解决了@Magnus提出的问题之后,请检查目录权限,以及您是否具有写入权限。如果有,请检查是否通过防病毒软件强制执行任何组策略。如果没有与在该文件夹中编辑该文件相关的特定策略,请检查文件类型是否有任何限制。希望这可以解决您的问题。