0

This is the code for my windows form. The goal of the form is to be able to preview a pdf on the application and then chose a folder with a drop down that you would like to copy the file to (this is what a "house" refers to in the code). This is a little project for work to make it simpler to organize files that get sent to the company. The listBox works populating with all the pdfs and the preview window works as well (this is the axAcroPDF component). But when hitting the send button to copy the file it says the file is being used by another process.

public partial class Form1 : Form { public Form1() { InitializeComponent();    
}
    string selectedPDF = "";
    string selectedHouse = "";
    DirectoryInfo currentPDF;
    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "PDF Files(*.pdf) |*.pdf;";
        openFileDialog1.ShowDialog();
        if(openFileDialog1.FileName != null)
        {
            axAcroPDF1.LoadFile(openFileDialog1.FileName);
        }

    }


    private void refreshBTN_Click(object sender, EventArgs e)
    {
        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\bkamide\Desktop\ExamplePDFS");
        FileInfo[] smFiles = dinfo.GetFiles("*.pdf");
        foreach (FileInfo fi in smFiles)
        {
            pdfList.Items.Add(Path.GetFileName(fi.Name));

        }
    }
    private void pdfList_SelectedIndexChanged(object sender, EventArgs e)
    {
        string firstSelectedItem = pdfList.SelectedItem.ToString();
        selectedPDF = firstSelectedItem;
        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Users\bkamide\Desktop\ExamplePDFS\" + firstSelectedItem);
        currentPDF = dinfo;
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        openFileDialog1.Reset();
        axAcroPDF1.EndInit(); 
        var sourceFile = new FileInfo(currentPDF.FullName);
        var dest = Path.Combine(@"C:\Users\bkamide\Desktop\ExamplePDFS\", selectedHouse, sourceFile.FullName);
        sourceFile.CopyTo(dest, true);
       
    }

    private void cbHouse_SelectedIndex(object sender, EventArgs e)
    {
        selectedHouse = cbHouse.SelectedIndex.ToString();
        
    }
}

I tried in the send method to reset the openFileDialog and axAcroPDF (not sure if I did that right) to see if those were the processes that could be using the file. I also tried restarting my computer to make sure nothing in the background was using it as well. I also did try the using method but I was not quite sure how to implement it within this.

quaabaam
  • 1,808
  • 1
  • 7
  • 15
  • You wrote _"This is the code for my windows form"_ So the `asp.net` Tag you added is a mistake and should be removed? – Stefan Wuebbe Jan 04 '23 at 21:59
  • Have you had a look at [this](https://stackoverflow.com/questions/6167136/how-to-copy-a-file-while-it-is-being-used-by-another-process)? – afarrag Jan 04 '23 at 22:06
  • Try to use [File.Copy](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.copy?source=recommendations&view=net-7.0) instead of allocating a FileInfo object (which involves overhead just for copying) – Mike93041 Jan 05 '23 at 12:54
  • I tried just going down to a bare bones program that simply uses File.Copy and nothing else and I am still getting the error. I have tried using some Windows tools to see what process was using the pdf but I could not find it. Problem must not be with the axAcroPDF previewer. – Benjamin Kamide Jan 05 '23 at 14:31
  • Edit: I tried this method using File.Move and it worked with no errors. – Benjamin Kamide Jan 05 '23 at 14:43

0 Answers0