简体   繁体   中英

Browse and save pdf files C# winform

I want to browse pdf files and store them in another folder. I have implemented pdf file browsing part. I can get all the pdf file path. Now I want to save them in an anther folder. Is there any way to do this?

    //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 ??
    }

How about

File.Copy(sourcePath, destinationPath);

Here is the full code snippet

//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);
    }
}

您可以使用File.Copy当你想将它们移动的方法,当你遍历所有PDF文件或File.Move

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM