Thanks for your reply.
Does the browser list the view, referencing the downloaded part?
I think that and the hint about the IPJ file (which was not referring to the temp folder) was the hint and the actual issue.
Unfortunately I wasn't able to reproduce it again since my first try when I recorded the screen capture.
If I will be able to reproduce the issue again, I will pay attention to that and update this thread.
Now I've set my goal to download the main file including all child references to the local Vault working folder and that seems to work as expected so far. I will not pass the temp folder anymore to my download method and force the "IFileManager.AcquireFiles()" method to download to the Vault working folder.
As I said, this way I wasn't able to reproduce the issue again.
This is my solution so far and maybe it will help others who will face the same issue in future:
internal FileAcquisitionResult DownloadFileSingle(Connection connection, ACW.File file, string folderPath)
{
FileAcquisitionResult downloadedFile = null;
try
{
SettingsHandler.log.Debug($"Directory={folderPath}");
if (string.IsNullOrWhiteSpace(folderPath))
{
// If no folder is passed, files will be downloaded to local Vault working folder
string vaultWorkingFolder = connection.WebServiceManager.DocumentService.GetRequiredWorkingFolderLocation();
if (vaultWorkingFolder.Contains("%"))
vaultWorkingFolder = ReplaceWindowsEnvironmentVariables(vaultWorkingFolder);
SettingsHandler.log.Debug($"No directory passed -> Files will be downloaded to local Vault working folder: {vaultWorkingFolder}");
}
// Delete file before, if it already exists
string localFileName = null;
if (!string.IsNullOrWhiteSpace(folderPath))
localFileName = Path.Combine(folderPath, file.Name);
else
{
localFileName = GetLocalFileName(connection, file);
if (localFileName.Contains("%"))
localFileName = ReplaceWindowsEnvironmentVariables(localFileName);
}
FileInfo fiExisting = new FileInfo(localFileName);
if (fiExisting.Exists)
{
SettingsHandler.log.Debug($"File already existing in target folder. File will be deleted before it will be downloaded from Vault... (FileName={fiExisting.FullName})");
fiExisting.IsReadOnly = false;
fiExisting.Delete();
SettingsHandler.log.Debug($"File deleted: {fiExisting.FullName}");
}
AcquireFilesSettings acquireSettings = new AcquireFilesSettings(connection);
if (!string.IsNullOrWhiteSpace(folderPath))
acquireSettings.LocalPath = new FolderPathAbsolute(folderPath);
else
acquireSettings.LocalPath = null; // download to working folder
acquireSettings.OptionsResolution.OverwriteOption = AcquireFilesSettings.AcquireFileResolutionOptions.OverwriteOptions.ForceOverwriteAll;
acquireSettings.OptionsResolution.SyncWithRemoteSiteSetting = AcquireFilesSettings.SyncWithRemoteSite.Always;
acquireSettings.OptionsRelationshipGathering.FileRelationshipSettings.VersionGatheringOption = VDF.Vault.Currency.VersionGatheringOption.Latest; // Get latest versions of file references
acquireSettings.OptionsRelationshipGathering.FileRelationshipSettings.RecurseChildren = true; // Get children of children
acquireSettings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeChildren = true; // Get children files
acquireSettings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeParents = false; // Do not get the parents of the file to process
acquireSettings.OptionsRelationshipGathering.FileRelationshipSettings.ReleaseBiased = false; // Get the latest version, not the latest released)
acquireSettings.OptionsRelationshipGathering.FileRelationshipSettings.IncludeLibraryContents = true; // Include library files
acquireSettings.OrganizeFilesRelativeToCommonVaultRoot = true; // Make sure to find all file references in expected folder
FileIteration fileIter = new FileIteration(connection, file);
acquireSettings.AddFileToAcquire(fileIter, AcquireFilesSettings.AcquisitionOption.Download);
AcquireFilesResults downloadedFiles = connection.FileManager.AcquireFiles(acquireSettings);
SettingsHandler.log.Debug($"FileResults.Count={downloadedFiles.FileResults?.Count()}");
if (downloadedFiles == null || downloadedFiles.FileResults == null || downloadedFiles.FileResults.Count() == 0)
throw new Exception($"Unexpected error downloading file to local directory (FileId={fileIter.EntityIterationId}, FileName={fileIter.EntityName}, FileVersion={fileIter.VersionNumber}, Directory={folderPath})");
if (downloadedFiles.FileResults.Any(x => x.Exception != null))
throw downloadedFiles.FileResults.First(x => x.Exception != null).Exception;
foreach (FileAcquisitionResult acquisitionResult in downloadedFiles.FileResults)
SettingsHandler.log.Debug($"{acquisitionResult.File.EntityName} -> {acquisitionResult.Status} (FullFileName={acquisitionResult.LocalPath}, NewFileIteration={acquisitionResult.NewFileIteration != null})");
downloadedFile = downloadedFiles.FileResults.FirstOrDefault(x => x.File.EntityName == file.Name && x.File.VersionNumber == file.VerNum);
if (downloadedFile != null)
{
foreach (FileAcquisitionResult fileAcquisitionResult in downloadedFiles.FileResults)
{
localFileName = fileAcquisitionResult.LocalPath.FullPath;
if (localFileName.Contains("%"))
localFileName = ReplaceWindowsEnvironmentVariables(localFileName);
m_downloadedFiles.Add(localFileName); // All files in this list will get deleted
}
if (!string.IsNullOrWhiteSpace(folderPath))
m_downloadPaths.Add(folderPath); // Folder will be deleted if it's not the Vault working folder
}
if (downloadedFiles.FileResults?.Count() > 1)
SettingsHandler.log.Debug($"File downloaded successfully with all {downloadedFiles.FileResults?.Count() - 1} references (FullFileName={downloadedFile?.LocalPath.FullPath})");
else
SettingsHandler.log.Debug($"File downloaded successfully (FullFileName={downloadedFile?.LocalPath.FullPath})");
}
catch (Exception ex)
{
SettingsHandler.log.Error(ex);
}
return downloadedFile;
}
internal string GetLocalFileName(Connection vaultConn, ACW.File file)
{
string vaultWorkingFolder = vaultConn.WebServiceManager.DocumentService.GetRequiredWorkingFolderLocation();
ACW.Folder folder = vaultConn.WebServiceManager.DocumentService.GetFolderById(file.FolderId);
string fileNameLocal = null;
if (vaultWorkingFolder.EndsWith("\\"))
fileNameLocal = Path.Combine(folder.FullName.Replace("/", "\\").Replace("$\\", vaultWorkingFolder), file.Name);
else
fileNameLocal = Path.Combine(folder.FullName.Replace("/", "\\").Replace("$", vaultWorkingFolder), file.Name);
return fileNameLocal;
}
internal string ReplaceWindowsEnvironmentVariables(string fullFileName)
{
// Regular expression to match any %<variable>% pattern
string pattern = @"%([^%]+)%";
// Use a MatchEvaluator to replace variables dynamically
string updatedPath = Regex.Replace(fullFileName, pattern, match =>
{
// Extract the environment variable name from the match
string variableName = match.Groups[1].Value;
// Get the value of the environment variable
string variableValue = Environment.GetEnvironmentVariable(variableName);
if (variableValue == null)
variableValue = Environment.GetEnvironmentVariable(variableName, EnvironmentVariableTarget.User);
if (variableValue == null)
variableValue = Environment.GetEnvironmentVariable(variableName, EnvironmentVariableTarget.Machine);
// If the variable is not found, return the original match (keep placeholder)
return string.IsNullOrEmpty(variableValue) ? match.Value : variableValue;
});
return updatedPath.Replace("\\\\", "\\");
}