[VB.NET] Error When Copying Files

Discussion in 'Programming General' started by matto, Apr 18, 2009.

[VB.NET] Error When Copying Files
  1. Unread #1 - Apr 18, 2009 at 5:14 AM
  2. matto
    Joined:
    Apr 2, 2007
    Posts:
    89
    Referrals:
    0
    Sythe Gold:
    0

    matto Member

    [VB.NET] Error When Copying Files

    Hi there,

    I have a program which copies a file to a directory. If the file exists, it renames it, then copies the file. However, I get this error:

    Could not find file "xxxxxxxxxx"

    It works on Vista, but on an XP VirtualMachine, I get that error. Any ideas?
     
  3. Unread #2 - Apr 18, 2009 at 6:37 AM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    [VB.NET] Error When Copying Files

    Giving us your code would help >_>
     
  5. Unread #3 - Apr 18, 2009 at 6:49 AM
  6. matto
    Joined:
    Apr 2, 2007
    Posts:
    89
    Referrals:
    0
    Sythe Gold:
    0

    matto Member

    [VB.NET] Error When Copying Files

    What it does:
    Checks if FILE.dll exists in the target directory. If it does, it renames it to FILE.dll.bak then copies FILE.dll from the current directory to target directory.
     
  7. Unread #4 - Apr 18, 2009 at 7:51 AM
  8. Stuart
    Joined:
    May 5, 2005
    Posts:
    1,580
    Referrals:
    2
    Sythe Gold:
    10

    Stuart Guru
    Banned

    [VB.NET] Error When Copying Files

    This code just seems to be full of random things that aren't even used...
    Code:
    Dim fs As New FileStream(Folder2 & "\FILE.dll", FileMode.Create, FileAccess.Write) 
    Never used...

    Code:
    My.Computer.FileSystem.RenameFile(TextBox1.Text, File & ".bak") 
    
    File isn't even declared.

    Try something like this.

    Code:
    Dim CurrentLocation As String = Application.StartupPath & "\FILE.dll"
            Dim DestinationFolder As String = Application.StartupPath
            If (FileIO.FileSystem.FileExists(CurrentLocation)) Then
                FileIO.FileSystem.CopyFile(CurrentLocation, DestinationFolder & "\FILE.dll.bak")
                If (FileIO.FileSystem.DirectoryExists(DestinationFolder)) Then
                    FileIO.FileSystem.CopyFile(CurrentLocation, DestinationFolder & "\FILE2.dll")
                Else
                    MsgBox("File cannot be copied because destination folder doesn't exist")
                End If
            Else
                MsgBox("File cannot be copied because the file doesn't exist")
            End If
    
     
  9. Unread #5 - Apr 18, 2009 at 8:33 AM
  10. matto
    Joined:
    Apr 2, 2007
    Posts:
    89
    Referrals:
    0
    Sythe Gold:
    0

    matto Member

    [VB.NET] Error When Copying Files

    EDIT*
    Here's what I would like my application to do.
    It's a patch for Steam. There are 2 files [Steam.dll] and [Steam.cfg] which can be placed in the Steam directory so that when you login all the games will be in your games list. I'm trying to make an application which creates a backup of the existing files, and then copies the patched ones into that directory.


    Code:
    My.Computer.FileSystem.RenameFile(TextBox1.Text, File & ".bak")
    That is because I want the user to select a file from a directory in "Program Files". This directory is stored in Textbox1.Text
    This is also the destination directory for when the file is copied.

    Code:
    Dim fs As New FileStream(Folder2 & "\FILE.dll", FileMode.Create, FileAccess.Write)
    Someone told me to first create the file, then copy it so I had that, but I've now removed it.

    Code:
    Dim CurrentLocation As String = Application.StartupPath & "\FILE.dll"
            Dim DestinationFolder As String = Application.StartupPath
            If (FileIO.FileSystem.FileExists(CurrentLocation)) Then
                FileIO.FileSystem.CopyFile(CurrentLocation, DestinationFolder & "\FILE.dll.bak")
                If (FileIO.FileSystem.DirectoryExists(DestinationFolder)) Then
                    FileIO.FileSystem.CopyFile(CurrentLocation, DestinationFolder & "\FILE2.dll")
                Else
                    MsgBox("File cannot be copied because destination folder doesn't exist")
                End If
            Else
                MsgBox("File cannot be copied because the file doesn't exist")
            End If
    If I'm not mistaken, this code copies Application.StartupPath\FILE.dll to DestinationFolder\FILE.dll.bak
    Then it copies it to DestinationFolder\FILE2.dll

    Is it possible to copy Application.StartupPath\FILE.dll to DestinationFolder\FILE.dll [But rename DestinationFolder\FILE.dll to DestinationFolder\FILE.dll.bak if it already exists]?
     
  11. Unread #6 - Apr 18, 2009 at 9:07 AM
  12. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    [VB.NET] Error When Copying Files

    I guess this does what you want:
    Code:
            Dim CurrentLocation As String = Application.StartupPath & "\FILE.dll"
            Dim DestinationFolder As String = Application.StartupPath & "\New Folder"
            If (FileIO.FileSystem.FileExists(CurrentLocation)) Then
    
                If (FileIO.FileSystem.FileExists(DestinationFolder & "\FILE.dll")) Then
                    FileIO.FileSystem.MoveFile(DestinationFolder & "\FILE.dll", DestinationFolder & "\FILE.dll.bak")
                End If
    
                FileIO.FileSystem.CopyFile(CurrentLocation, DestinationFolder & "\FILE.dll")
            End If
    
     
  13. Unread #7 - Apr 18, 2009 at 10:11 AM
  14. matto
    Joined:
    Apr 2, 2007
    Posts:
    89
    Referrals:
    0
    Sythe Gold:
    0

    matto Member

    [VB.NET] Error When Copying Files

    Thanks a lot! Perfect :) BTW I am so jealous of your PC specs >. Is there a way to embed FILE.dll in the resources so instead of having it in Application.StartupPath & "\FILE.dll" it's embedded in the program itself? This way I only need to send 1 file rather than the exe and FILE.dll
     
  15. Unread #8 - Apr 18, 2009 at 3:15 PM
  16. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    [VB.NET] Error When Copying Files

    np :)
    of course you can embed the file! I was experimenting and my result was this;
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim stream As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("vb.FILE.dll") '*Your namespace*.*file name*
            Dim fileStream As New System.IO.FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop).ToString() + "\test.dll", IO.FileMode.Create) ' test.dll is the output file name which is saved to the desktop
            Dim binaryReader As New System.IO.BinaryReader(stream) 'Use a binary reader to read the stream from the embeded resource
            Dim binaryWriter As New System.IO.BinaryWriter(fileStream) 'use a binary writer to write the final output (write it to the file stream)
    
            binaryWriter.Write(binaryReader.ReadBytes(stream.Length)) 'write everything that is read from the resource to the output file stream
    
    
            'Clean up by closing everything
            binaryReader.Close()
            binaryWriter.Close()
            fileStream.Close()
            stream.Close()
    
        End Sub
    
    you can edit it to use it the other code...

    and if you don't know how to add and embed a file in the .exe file read this;
    1. Double click "My Project" under your project name in solution explorer
    2. Click the "Resources" tab
    3. Click the arrow to the right of the "Add Resource" button on the top
    4. Click "Add Existing File..." in the menu
    5. In the open file dialog that apears, make sure the "All Files (*.*)" filter is selected and then navigate to and open the file you want
    6. Now, in solution explorer you will see the file. Click on it once to select it and then go to the properties and select "Embedded resource" in the "Build Action" drop down list
    7. You are ready to build/debug your project and it should work like a charm :)
     
  17. Unread #9 - Apr 18, 2009 at 3:19 PM
  18. matto
    Joined:
    Apr 2, 2007
    Posts:
    89
    Referrals:
    0
    Sythe Gold:
    0

    matto Member

    [VB.NET] Error When Copying Files

    OmG mE wAnTiEz Ur BABIEZZZ!!!!!

    Thank you sooo much!!!
     
  19. Unread #10 - Apr 18, 2009 at 3:21 PM
  20. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    [VB.NET] Error When Copying Files

    lol :laugh:
    No problem.. I'm glad to help :)
     
< Need Treeview & Get/SaveSetting help | form2 location >

Users viewing this thread
1 guest


 
 
Adblock breaks this site