none
File.Copy Simply Doesn't Work by Itself VS2012 Excel RRS feed

  • Question

  • Simply copying a file from one directory to another using file.copy(sourcefilename, destfilename, true) never works because the sourcefile is always in use! Always!

    If I run this simple code in debug it works fine, but without debug the source file is ALWAYS in use.  I used the file.copy method back in VS2005 on xml documents and it worked fine back then.  Now, using Excel files, it NEVER works.

    Clearly I'm missing something.


    Ross

    Wednesday, December 31, 2014 4:52 PM

Answers

  • Ok, perhaps I left out a key component.  The file.copy method runs as a result of a FileSystemWatcher on a specific folder.  When I drag and drop the original file, the FSW executes on create, so I guess the FSW is what has the file in use; only, if that were the case, then why does it work in debug mode?

    Thanks.


    Ross

    • Marked as answer by T3mplar Friday, January 2, 2015 5:34 PM
    Wednesday, December 31, 2014 6:04 PM

All replies

  • I assume you are missing something. I created a test project with the following.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        Dim sourcePath As String = IO.Path.Combine(folder, "Test.xlsx")
        Dim destPath As String = IO.Path.Combine(folder, "ExcelTest", "Test.xlsx")
        IO.File.Copy(sourcePath, destPath, True)
    End Sub
    

    As long as Test.xlsx exists in My Documents and there is a folder called ExcelTest also in My Documents, the file is copied to the ExcelTest folder. It works both within Visual Studio and when running the compiled exe outside Visual Studio.

    Is it possible that the file really is in use?

    Wednesday, December 31, 2014 5:30 PM
  • Ok, perhaps I left out a key component.  The file.copy method runs as a result of a FileSystemWatcher on a specific folder.  When I drag and drop the original file, the FSW executes on create, so I guess the FSW is what has the file in use; only, if that were the case, then why does it work in debug mode?

    Thanks.


    Ross

    • Marked as answer by T3mplar Friday, January 2, 2015 5:34 PM
    Wednesday, December 31, 2014 6:04 PM
  • Ok, perhaps I left out a key component.  The file.copy method runs as a result of a FileSystemWatcher on a specific folder.  When I drag and drop the original file, the FSW executes on create, so I guess the FSW is what has the file in use; only, if that were the case, then why does it work in debug mode?

    I'm afraid I don't know much about FileSystemWatcher. I do think that it is reasonable to assume that if File.Copy fails saying the file is in use, the file must be in use. Perhaps you should ask a new question about FileSystemWatcher?
    Wednesday, December 31, 2014 7:11 PM
  • Hello Ross,

    Have you check if you run application without the effect of the FileSystemWatcher Class as simplely as the code Blackwood provided? If not, it is recommended to do that which could help narrow down this issue.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 1, 2015 5:56 AM
    Moderator
  • I just gave up with that approach and went a different direction.

    Thanks.


    Ross

    Friday, January 2, 2015 5:34 PM