Beispiel #1
0
        private void InsertAttachment(AttachmentCollection attachments)
        {
            if (attachments.Count != 0)
            {
                //SetMetadataText("\tAttachments:");
                SetMetadataText("\t" + ResourceHelper.GetResourceString("WORD_REPORT_ATTACHMENTS"));
                var webClient = new TfsWebClient(tfsManager.collection);

                string dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
                Directory.CreateDirectory(dir);
                string zipFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N") + ".zip");

                // AttachmentsParagraph = null;

                try
                {
                    string filePath = string.Empty;
                    foreach (Attachment attachment in attachments)
                    {
                        string fileName = attachment.Name;

                        filePath = Path.Combine(dir, attachment.Name);
                        //process attachments with same names
                        var counter = 0;
                        while (File.Exists(filePath) && counter < 100) //up to 100 items
                        {
                            counter++;

                            if (counter == 1)
                            {
                                filePath = filePath.Insert(filePath.LastIndexOf(".", StringComparison.Ordinal), string.Format("({0})", counter));
                            }
                            else
                            {
                                filePath = filePath.Replace(string.Format("({0}).", counter - 1), string.Format("({0}).", counter));
                            }
                        }

                        try
                        {
                            webClient.DownloadFile(attachment.Uri, filePath);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, ResourceHelper.GetResourceString("ERROR_TEXT"), MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, ResourceHelper.GetResourceString("ERROR_TEXT"), MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    OfficeHelper.CompressDirectory(dir, zipFileName);
                    InsertFile(zipFileName, "Attachments.zip");
                    File.Delete(zipFileName);
                    Directory.Delete(dir, true);
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (((Button)e.Source).Tag == null) return;
            var url = ((Button)e.Source).CommandParameter.ToString();
            var issueId = ((Button)e.Source).Tag.ToString();
            if (url != "" && url.ToLower().EndsWith(".png"))
            {
                using (UIHost.GetWaitCursor())
                {
                    // Ensure the issue folder exists
                    string issueFolder = Path.Combine(IssuesFolderName, issueId);
                    if (!Directory.Exists(issueFolder))
                    {
                        Directory.CreateDirectory(issueFolder);
                    }
                    // Download and open the pic
                    var fileName = url;
                    int fnIdx = fileName.IndexOf("FileName=");
                    fileName = fileName.Substring(fnIdx + "FileName=".Length);
                    var localFileName = Path.Combine(issueFolder, fileName);
                    using (TfsWebClient client = new TfsWebClient(TeamProjectCollection))
                    {
                        client.DownloadFile(url, localFileName);
                    }
                    System.Diagnostics.Process.Start(localFileName);
                }
            }
            if (url != "" && url.ToLower().EndsWith(".zip"))
            {

                var tempFile = System.IO.Path.GetTempFileName();
                using (UIHost.GetWaitCursor())
                {
                    // Ensure the issue folder exists
                    string issueFolder = Path.Combine(IssuesFolderName, issueId);
                    if (!Directory.Exists(issueFolder))
                    {
                        Directory.CreateDirectory(issueFolder);
                    }
                    // Download and unzip the file
                    try
                    {
                        using (TfsWebClient client = new TfsWebClient(TeamProjectCollection))
                        {
                            client.DownloadFile(url, tempFile);
                            System.IO.Compression.ZipFile.ExtractToDirectory(tempFile, issueFolder);
                        }
                    }
                    finally
                    {
                        if (File.Exists(tempFile))
                        {
                            File.Delete(tempFile);
                        }
                    }
                }
            }
        }