copy file to directory in visual basic vb.net - 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

Have you read the documentation for File.Copy, or even just paid attention to Intellisense? Both arguments must be file paths. Neither can be folder paths.
On a related note, why do you have a variable named 'sourceDir' when it's clearly a file path and not a directory path? If you name things clearly - and particularly not misleadingly - then it's more likely that you'll avoid such mistakes. Of course, using the Help menu or F1 key to confirm that you're using a type of method correctly would help too.

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

Related

i can't access a file path vb.net

i really don't understand , the code is clear and simple , my app throw exception when i want to read the path of an existing file .
the error : "the given path's format is not supported"
https://i.stack.imgur.com/rXNTN.png
even if i changed the path it's the same problem
that's my code :
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo("‪C:\Users\ochallal\Desktop\ENQ1620_3.sdf")
Dim folderPath As String = testFile.DirectoryName
Dim fileName As String = testFile.Name
Dim fullPath As String
fullPath = My.Computer.FileSystem.CombinePath(folderPath, fileName)
and that's the details of the exception
https://i.stack.imgur.com/njJcv.png
i'm guessing that it could be permission stuff but i have all permissions and using visual studio as Administartor
When I copy your text and paste it in Visual Studio, and look with a hex editor at it I see some extra bytes in the string:
Remove that and it will work.

how do I use a document using debug folder (vb.net)

I am trying to show a document (called zzz.txt so it appears at the bottom) in a text box, and I have put it in the debug folder (TextViewerthingy > obj > Debug) and have used CurDir() & "\zzz.txt" for the location, which apparently is not correct. I have used this correctly with other projects
Dim filename As String = CurDir() & "\zzz.txt"
Dim ObjReader As New System.IO.StreamReader(filename)
gives me an error saying the file is not there. This is the location, what is it that I'm doing wrong in this case?
C:\Users\Notshowingmyname\source\repos\TextViewerthingy\TextViewerthingy\bin\Debug
Try this one :
Imports System.IO
Dim filename as string = Directory.GetCurrentDirectory() + "\zzz.txt"
Or second alternative :
filename = My.Computer.FileSystem.CurrentDirectory + "\zzz.txt"
Make sure file name and extensions are correct.

Cannot retrieve path of Special Folders when redirection enabled

I have a tool that reads a .txt file for a list of paths and uses them in a copy. It looks like this:
***DESTINATION***
E:\Backup
***Sources***
%USERPROFILE%\Pictures
%USERPROFILE%\Favourites
%USERPROFILE%\Contacts
%USERPROFILE%\My Videos
My system has folder redirection enabled so 'Pictures' for example is actually D:\Adam\Pictures. However when using the following code it will only resolve as C:\Adam\Pictures and throw a "Cannot find path error".
'Declarations earlier in script
Dim Destpath As String = System.IO.File.ReadAllLines(Application.StartupPath + "\CONFIG.txt")(1)
Dim userprofilevar = (Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
'Snippet of line reading logic
ElseIf line.Contains("%USERPROFILE%") Then
Dim lineArray() As String = line.Split("\")
Dim lineuser As String = userprofilevar + "\"
Dim linepath As String = lineArray(1)
line = lineuser + linepath
Destpath = String.Concat(Destpath, "\", linepath)
MessageBox.Show(Destpath)
Else
Does anyone know how to resolve the correct path of the redirected folder through the %USERPROFILE% variable?
Solution I found:
After converting #AlexB's example into VB.NET and getting it working in my app, I simply declared each of the %Userprofile% folders as a string then have the line reading logic update the line string as the correct folder path:
'Declarations as start of Form
Dim downloadsPath As String = KnownFolders.GetPath(KnownFolder.Downloads)
Dim contactspath As String = KnownFolders.GetPath(KnownFolder.Contacts)
'Snippit of line reading logic
ElseIf line.Contains("%USERPROFILE%\Downloads") Then
line = downloadspath
ElseIf line.Contains("%USERPROFILE%\Contacts") Then
line = contactspath
It's not pretty to do for all 11 %Userprofile% folders, but it works!

VB.net use string in Directory Path?

I apologise for my lack of basic VB.net knowledge but I'm looking to use the equivalent of %systemdrive% to find the drive containing Windows to check for an existing directory in VB.net - so I have the below.
Dim systemPath As String = Mid(Environment.GetFolderPath(Environment.SpecialFolder.System), 1, 3)
If Not Directory.Exists("'systemPath'\MyFolder") Then
Directory.CreateDirectory("'systemPath'\MyFolder")
End If
Can someone help with using the systemPath string in the directory query? Thank you.
Well you should write
Dim systemPath As String = Mid(Environment.GetFolderPath(Environment.SpecialFolder.System), 1, 3)
If Not Directory.Exists(Path.Combine(systemPath, "MyFolder")) Then
Directory.CreateDirectory(Path.Combine(systemPath, "MyFolder"))
End If
You could get the environment variable called %SYSTEMDRIVE% with Environment.GetEnvironmentVariable, but then the results obtained should be manually combined with current directory separator char ("\") because I have not found any way to convince Path.Combine to build a valid path with only the system drive (I.E. C: )
Dim sysDrive = Environment.GetEnvironmentVariable("SystemDrive")
Dim myPath = sysDrive & Path.DirectorySeparatorChar & "MyFolder"
IO.Path has methods that should be used IMHO
Dim sysDrive As String = Environment.GetEnvironmentVariable("SystemDrive") & IO.Path.DirectorySeparatorChar
Dim myPath As String = IO.Path.Combine(sysDrive, "FolderName")

Could not find a part of the path during File.Copy

I'm trying to copy a file to another directory but I'm getting this error
System.IO.DirectoryNotFoundException: Could not find a part of the
path 'C:\Documents and
Settings\(my username)\Desktop\Source_Folder\File_1.xlsx'.
But File_1.xlsx exists in the folder Source_Folder which exists on my Desktop. So why am I getting this error?
Here is my code
Dim path_orig As String = "C:\Documents and Settings\(my username)\Desktop\Source_Folder\"
Dim oldFile As String = System.IO.Path.Combine(path_orig, filename)
Dim path_new As String = Server.MapPath("~") & "\Destination_Folder\"
Dim newFile As String = System.IO.Path.Combine(path_new, filename)
File.Copy(oldFile, newFile, True)
*filename is a variable which in this case is "File_1.xlsx"
Don't hard code Documents and Settings. It will change by OS. Use this instead:
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Source_Folder")
Also if you're running this in a web application you're likely running into permissions problems. Change the location where you're storing files and/or change the user the app pool is running under and/or grant the app pool user rights to the folder.