【问题标题】:How can I prompt a user to choose a location to save a file?如何提示用户选择保存文件的位置?
【发布时间】:2011-05-19 11:53:17
【问题描述】:

在我的主表单中,我有一个名为 SavePDFDocument() 的方法:

private void SavePDFDocument()
{
    PDFWrapper pdfWrapper = new PDFWrapper();
    pdfWrapper.CreatePDF(horizontalPictureScroller1.GetPictures(), "pdfDocument.pdf");
}

如您所见,现在我正在手动输入文件名。我想请用户选择保存它的位置以及给它起什么名字。

这是我上面使用的 CreatePDF() 方法:

public void CreatePDF(List<System.Drawing.Image> images, string filename)
{
    if (images.Count >= 1)
    {
        Document document = new Document(PageSize.LETTER);
        try
        {

            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file

            PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));

            // step 3: we open the document
            document.Open();

            foreach (var image in images)
            {
                iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);

                if (pic.Height > pic.Width)
                {
                    //Maximum height is 800 pixels.
                    float percentage = 0.0f;
                    percentage = 700 / pic.Height;
                    pic.ScalePercent(percentage * 100);
                }
                else
                {
                    //Maximum width is 600 pixels.
                    float percentage = 0.0f;
                    percentage = 540 / pic.Width;
                    pic.ScalePercent(percentage * 100);
                }

                pic.Border = iTextSharp.text.Rectangle.BOX;
                pic.BorderColor = iTextSharp.text.BaseColor.BLACK;
                pic.BorderWidth = 3f;
                document.Add(pic);
                document.NewPage();
            }
        }
        catch (DocumentException de)
        {
            Console.Error.WriteLine(de.Message);
        }
        catch (IOException ioe)
        {
            Console.Error.WriteLine(ioe.Message);
        }

        // step 5: we close the document
        document.Close();
    }
}

有什么建议吗?

【问题讨论】:

    标签: c# savefiledialog


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      你看过SaveFileDialog吗?

      private void button1_Click(object sender, System.EventArgs e)
      {
           Stream myStream ;
           SaveFileDialog saveFileDialog1 = new SaveFileDialog();
      
           saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
           saveFileDialog1.FilterIndex = 2 ;
           saveFileDialog1.RestoreDirectory = true ;
      
           if(saveFileDialog1.ShowDialog() == DialogResult.OK)
           {
               if((myStream = saveFileDialog1.OpenFile()) != null)
               {
                   // Code to write the stream goes here.
                   myStream.Close();
               }
           }
      }
      

      【讨论】:

        【解决方案3】:

        我相信this page 描述了您正在寻找的内容:

        // Configure save file dialog box
        Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
        dlg.FileName = "Document"; // Default file name
        dlg.DefaultExt = ".text"; // Default file extension
        dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension
        
        // Show save file dialog box
        Nullable<bool> result = dlg.ShowDialog();
        
        // Process save file dialog box results
        if (result == true)
        {
            // Save document
            string filename = dlg.FileName;
        }
        

        【讨论】:

        • 我认为更高级别的“System.Windows.Forms.SaveFileDialog”在这种情况下更合适。有谁碰巧知道有什么区别?看了MSDN后不明显。
        • 谢谢,不过这个问题比较难。如何将此 SaveFieldDialog 功能与上面的代码集成?请注意,我正在使用 New FileStream()。有什么建议吗?
        • @Serg 查看我在答案中发布的示例代码;它正在处理一个 Stream 对象。
        • @Nathan - 我也很想知道这些差异。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-06-21
        • 1970-01-01
        • 2014-03-10
        • 1970-01-01
        • 2021-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多