Search code examples
vb.netvisual-studio-2017file-copying

copy file to directory in visual basic vb.net


I am tringing to copy files settings.copy from sourceDir to backupDir but getting error

Dim sourceDir As String = "c:\in\settings.copy"

Dim backupDir As String = "c:\out\"

File.Copy(sourceDir, backupDir)

while executing above script getting below error

System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'c:\out\'.'

I already created c:\out\ folder


Solution

  • Dim userprofile As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

    Dim SystemDir As String = Environment.GetEnvironmentVariable("SystemDrive")

    Dim sourceDir As String = "y\inbound\settings.exe"

    Dim backupDir As String = "AppData\Local\user\default_user\"

    Dim root As String = Path.GetPathRoot(userprofile)

    Dim useDrpath As String = Path.Combine(userprofile, backupDir)

    Dim SysDrpath As String = Path.Combine(SystemDir, root, sourceDir)

    Dim file = New FileInfo("settings.cps")

    file.CopyTo(Path.Combine(SysDrpath, useDrpath, file.Name), True)

    My gole is to copy file from system installed driver to user profile driver

    with above code i am able to copy file

    c:\y\inbound\settings.exe C:\Users\pavan\AppData\Local\user\default_user\

    please suggested any other better way to do above