Пример #1
0
        public void CreateAttachment(Attachment attachment)
        {
            if (attachment == null)
            {
                throw new ArgumentNullException("attachment");
            }

            var wiql = string.Format(CultureInfo.InvariantCulture, "SELECT [System.Id] FROM WorkItems WHERE [System.Id] = {0}", attachment.WorkItemId);

            var workItem = this.QueryWorkItems(wiql)
                               .Cast<TeamFoundation.WorkItemTracking.Client.WorkItem>()
                               .FirstOrDefault();

            if (workItem == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "There is not any Work Item with an Id equals to {0}", attachment.WorkItemId), "attachment.WorkItemId");
            }

            var path = Path.Combine(Path.GetTempPath(), attachment.Id);
            var fileInfo = new FileInfo(path);

            if (!string.IsNullOrWhiteSpace(attachment.Name))
            {
                var fileName = string.IsNullOrWhiteSpace(attachment.Extension)
                                ? attachment.Name
                                : attachment.Extension.StartsWith(".", StringComparison.OrdinalIgnoreCase)
                                    ? string.Format(CultureInfo.InvariantCulture, "{0}{1}", attachment.Name, attachment.Extension)
                                    : string.Format(CultureInfo.InvariantCulture, "{0}.{1}", attachment.Name, attachment.Extension);

                path = Path.Combine(Path.GetTempPath(), fileName);

                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                fileInfo.MoveTo(path);
            }

            workItem.Attachments.Add(attachment.ToEntity(path));
            workItem.Save();

            fileInfo.Delete();
        }