浏览并保存pdf文件C#winform
本文关键字:文件 C#winform pdf 保存 浏览 | 更新日期: 2023-09-27 18:29:05
我想浏览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 ??
}
怎么样
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);
}
}
当您循环浏览所有pdf时,或者当您想将它们移动到File.Move
时,可以使用File.Copy
方法