0

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

2 に答える 2

4

どうですか

File.Copy(sourcePath, destinationPath);

ここに完全なコードスニペットがあります

//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);
    }
}
于 2013-08-25T11:50:25.740 に答える