Results 1 to 9 of 9

Thread: Help with CopyFile

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Unhappy Help with CopyFile

    I am trying toi run the below code but it is giving error.

    Code:
    Dim BG_Logon As String
            OpenFileDialog1.ShowDialog()
            BG_Logon = OpenFileDialog1.FileName
            My.Computer.FileSystem.CopyFile(BG_Logon, "e:\sss\sss.jpg")
    Error: The file 'e:\sss\sss.jpg' already exists.

    I have checked and there is no such file in E:\sss, no matter wherever I copy this file error remains the same. Any ideas?
    < advertising link removed by moderator >

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with CopyFile

    The file must exist or you wouldn't get that error message, unless something is seriously wrong with your system. Do you by any chance have code before that that might open a file at that location?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: Help with CopyFile

    I added a overwrite command but still no success in the system32 drive.



    Code:
    Imports Microsoft.Win32
    
    Public Class Form1
        Dim regkey As RegistryKey
        Dim BG_Logon As String
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            OpenFileDialog1.ShowDialog()
            BG_Logon = OpenFileDialog1.FileName
            My.Computer.FileSystem.CopyFile(BG_Logon, Environ("&#37;systemdrive%") & "\system32\oobe\info\backgrounds\defaultbackground.jpg", overwrite:=1)
    
    
        End Sub
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            regkey = Registry.LocalMachine.OpenSubKey("Software", True)
            regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background", True)
            regkey.SetValue("OEMBackground", 1)
            regkey.Close()
    
            My.Computer.FileSystem.WriteAllBytes(Environ("%systemdrive%") & "\md.cmd", My.Resources.md, 0)
            Process.Start(Environ("%systemdrive%") & "\md.cmd")
    
    
        End Sub
    
        
       
    End Class
    < advertising link removed by moderator >

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Help with CopyFile

    Shall we assume that the file really did exist then?
    You can not overwrite a file that is already open/locked in another application.

    'No success' does not mean anything, explain the error.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help with CopyFile

    Quote Originally Posted by jmcilhinney View Post
    Do you by any chance have code before that that might open a file at that location?
    You never did answer this question.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: Help with CopyFile

    No, I haven't done that Check out my code, I written copy command after the file is opened.
    < advertising link removed by moderator >

  7. #7
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564

    Re: Help with CopyFile

    Why do you open it before you copy it?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    76

    Re: Help with CopyFile

    Quote Originally Posted by BrianS View Post
    Why do you open it before you copy it?
    OOPs my bad

    But it still it won't work can some1 do the coding for me
    < advertising link removed by moderator >

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Help with CopyFile

    Asking someone to 'do' the coding for you is like asking someone to make you a cake without telling you what type of cake to make. Your code looks OK, what you need to do is explain where the problem is.
    Your 1st code example is different from your 2nd code example.
    I will attempt to explain how you solve your own problem for yourself.
    Try making a simple copy of a file you know exists to a place where you know the file does not to ensure you know how to use the command. With this, I mean using names you know can not fail, hardcoded filenames. Do not allow other code to run/get involved in this, it will confuse and maybe give you false results. For example:

    Code:
    My.Computer.FileSystem.CopyFile("c:\testfile.jpg", "c:\outputfile.jpg",True)
    Run this a few times, if this works for you then you know your command use is correct. From there you can replace the parameters with variables:

    Code:
    My.Computer.FileSystem.CopyFile(BG_Logon, "c:\outputfile.jpg",True)
    Now, if the file copy has worked you should be able to see it. If you have an error, then your source file is invalid as your first test worked over and over again. If you are running other code at the same time then your test results may not be accurate

    Now you replace the other variable:

    Code:
    My.Computer.FileSystem.CopyFile(BG_Logon,  Environ("&#37;systemdrive%") & "\system32\oobe\info\backgrounds\defaultbackground.jpg,True)
    If this works on its own, then something else in your program is causing the issue. If this does not work, then your filename you are trying to save to is invalid. You should look at your values before using them to ensure you have decent parameters.

    Code:
    Msgbox  Environ("%systemdrive%") & "\system32\oobe\info\backgrounds\defaultbackground.jpg"
    What does it look like, do you have 2 backslashes? \\


    Once you have done all that you should know where the problem lies, the source params, the destination params, or something else in your code. If its something else, your gonna have to show us what else you do in your app.

    If its your filenames, maybe you should look into using the IO.Path.Combine method instead like so:

    Code:
    IO.Path.Combine( Environ("%systemdrive%") , "system32\oobe\info\backgrounds\defaultbackground.jpg")
    If I use this Environ("%systemdrive%") I get back a nothing, so thats a consideration to me. However Environ("systemdrive") returns "C:" combined with your hard coded path will still not give me the correct location of my system32 directory.

    Hopefully you can look into why you have a failure and if you still have problems can explain more clearly what/where you are having issues.
    Last edited by Grimfort; May 13th, 2010 at 11:30 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width