• Log in or sign up for an account here: https://www.sythe.org/login

[VB.NET] Error When Copying Files

matto

Member
Joined
Apr 2, 2007
Sythe Gold
0
Messages
89
[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?
 
[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?
Giving us your code would help >_>
 
[VB.NET] Error When Copying Files
Dim FileToCopy As String
Dim MoveLocation As String
FileToCopy = CurDir() & "\FILE.dll"
MoveLocation = Folder2 & "\FILE.dll"

If System.IO.File.Exists(Folder2 & "\FILE.dll") = True Then
My.Computer.FileSystem.RenameFile(TextBox1.Text, File & ".bak")
ProgressBar1.PerformStep()
Dim fs As New FileStream(Folder2 & "\FILE.dll", FileMode.Create, FileAccess.Write)
System.IO.File.Move(FileToCopy, MoveLocation)
ProgressBar1.PerformStep()
Else
System.IO.File.Move(FileToCopy, MoveLocation)
ProgressBar1.Value = 100
End If

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.
 
[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
 
[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]?
 
[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
 
[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
 
[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
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 :)
 
[VB.NET] Error When Copying Files
OmG mE wAnTiEz Ur BABIEZZZ!!!!!

Thank you sooo much!!!
 
Runescape Gamblingrunescape gambling
Back
Top