public void AddFile(IMetadataItem metadataItem, bool deleteFile) { var repositoryDir = _appSettingService.GetRepositoryDir(); var year = metadataItem.ValutaDatum.Year; var documentId = Guid.NewGuid(); var extension = Path.GetExtension(metadataItem.OrginalPath); var contentFileName = _filenameGenerator.GetContentFilename(documentId, extension); var metadataFileName = _filenameGenerator.GetMetadataFilename(documentId); var targetDir = Path.Combine(repositoryDir, year.ToString()); metadataItem.ContentFileExtension = extension; metadataItem.ContentFilename = contentFileName; metadataItem.MetadataFilename = metadataFileName; metadataItem.DocumentId = documentId; metadataItem.ValutaYear = year.ToString(); if (!Directory.Exists(targetDir)) { Directory.CreateDirectory(targetDir); } WriteXml(targetDir, metadataFileName, metadataItem); //move file File.Copy(metadataItem.OrginalPath, Path.Combine(targetDir, contentFileName)); if (deleteFile) { var task = Task.Factory.StartNew( () => { Task.Delay(500); File.Delete(metadataItem.OrginalPath); }); try { Task.WaitAll(task); } catch (Exception e) { MessageBox.Show(e.Message); } } }