cp100
cp100

Reputation: 1493

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

Upvotes: 0

Views: 8119

Answers (2)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48568

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

Upvotes: 4

Venson
Venson

Reputation: 1870

You could use the File.Copy method when you loop through all pdf's or when you want to move them File.Move

Upvotes: 1

Related Questions