The error in the title appears when this code is executed:
using (var stream = new System.IO.FileStream(downloadPath + file.Id, System.IO.FileMode.Create))
{
request.Download(stream);
}
specifically the line:
using (var stream = new System.IO.FileStream(downloadPath + file.Id, System.IO.FileMode.Create))
The purpose of this code is to download multiple spreadsheets and parse out the information to a database. This code executes normally for about 15 of the spreadsheets (there are about 100 - it differs with the day and what is input by remote users).
It accesses a shared google drive and loops through gathering the spreadsheets to parse out.
What would be causing it to create this error?
Here is the entire code:
public Dictionary<string,String[]> DownloadSpreadsheetsReturnLinks(string downloadPath, string[] sourceFolders = null, string[] spreadsheetNames = null)
{
try
{
if (!System.IO.Directory.Exists(downloadPath))
System.IO.Directory.CreateDirectory(downloadPath);
else
System.IO.Directory.EnumerateFiles(downloadPath).ToList().ForEach(f => System.IO.File.Delete(f));
Dictionary<string, string> folderList = null;
if (sourceFolders != null)
{
var folders = GetFolderList();
folderList = new Dictionary<string, string>();
foreach (var folder in folders)
{
if (sourceFolders.Contains(folder.Name))
{
folderList.Add(folder.Id, folder.Name);
}
}
}
var fileLinks = new Dictionary<string, String[]>();
var spreadsheets = GetFileList(Constants.SpreadsheetMimeType);
foreach (var file in spreadsheets)
{
//dont download items in the trash
if (file.Trashed.Value)
continue;
//if we have folders then check the file is in one
if (folderList == null || IsFileInFolder(file, folderList))
{
if (spreadsheetNames == null || spreadsheetNames.Contains(file.Name))
{
//add our file to the dictionary
fileLinks.Add(file.Id, new string[] { file.Name, file.WebViewLink });
//var request = _driveService.Files.Export(file.Id, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
var request = _driveService.Files.Export(file.Id, Constants.SpreadsheetMimeType2);
using (var stream = new System.IO.FileStream(downloadPath + file.Id, System.IO.FileMode.Create))
{
request.Download(stream);
}
}
}
}
return fileLinks;
}
catch (Exception ex)
{
throw ex;
}
}