【问题标题】:Browse and save pdf files C# winform浏览和保存 pdf 文件 C# winform
【发布时间】:2013-08-28 00:42:54
【问题描述】:

我想浏览 pdf 文件并将它们存储在另一个文件夹中。我已经实现了pdf文件浏览部分。我可以获得所有的pdf文件路径。现在我想将它们保存在另一个文件夹中。 有没有办法做到这一点?

    //Keep pdf file locations
    List<string> pdfFiles = new List<string>();

    // Browse pdf and get their paths
    private void btnPdfBrowser_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.CheckFileExists = true;
        openFileDialog.AddExtension = true;
        openFileDialog.Multiselect = true;
        openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";

        if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            foreach (string fileName in openFileDialog.FileNames)
            {
                pdfFiles.Add(fileName);
            }
        }
    }

    private void btnUploadFile_Click(object sender, EventArgs e)
    {
        string installedPath = Application.StartupPath + "pdf";

        //Check whether folder path is exist
        if (!System.IO.Directory.Exists(installedPath))
        {
            // If not create new folder
            System.IO.Directory.CreateDirectory(installedPath);
        }
        //Save pdf files in installedPath ??
    }

【问题讨论】:

    标签: c# winforms pdf file-upload save


    【解决方案1】:

    怎么样

    File.Copy(sourcePath, destinationPath);
    

    这里是完整的代码sn-p

    //Keep pdf file locations
    List<string> pdfFiles = new List<string>();
    
    // Browse pdf and get their paths
    private void btnPdfBrowser_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.CheckFileExists = true;
        openFileDialog.AddExtension = true;
        openFileDialog.Multiselect = true;
        openFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
    
        if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pdfFiles = new List<string>();  
            foreach (string fileName in openFileDialog.FileNames)
                pdfFiles.Add(fileName);
        }
    }
    
    private void btnUploadFile_Click(object sender, EventArgs e)
    {
        string installedPath = Application.StartupPath + "pdf";
    
        //Check whether folder path is exist
        if (!System.IO.Directory.Exists(installedPath))
        {
            // If not create new folder
            System.IO.Directory.CreateDirectory(installedPath);
        }
        //Save pdf files in installedPath
        foreach (string sourceFileName in pdfFiles) 
        {
            string destinationFileName = System.IO.Path.Combine(installedPath, IO.Path.GetFileName(sourceFileName));
            System.IO.File.Copy(sourceFileName, destinationFileName);
        }
    }
    

    【讨论】:

      【解决方案2】:

      当您遍历所有 pdf 或想要移动它们时,您可以使用 File.Copy 方法File.Move

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        • 2013-10-14
        • 1970-01-01
        • 2016-06-05
        • 1970-01-01
        • 1970-01-01
        • 2019-02-21
        相关资源
        最近更新 更多