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

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.

Related

VBA FileExists and Sharepoint

I'm running into issues trying to pull info from files stored in Sharepoint.
Namely, FileExists isn't working and Overwrite file doesn't seem to be working either.
There was a discussion here, but few answers -> posting this question again in hopes some things have changed
My code runs like this:
strFileExists = Dir(Filepath & Filename)
And returns: File path not found -> I checked the path and even opened a file and recorded the macro to make sure it was the same file path without issue, but it appears DIR() is the issue.
The business dept I'm working with is entirely switching over to Sharepoint so hoping there's a straightforward solution without setting up network shares or doing C/personal/OneDrive things
You can navigate and look for files on OneDrive like this
Sub check_File_Exists()
Dim path As String
Dim strType As String
Dim file As Variant
Dim yourFile As String
'replace uname with your user name
path = "C:\Users\uname\OneDrive\"
strType = "*txt"
yourFile = "test.txt"
file = Dir(path & strType)
Do While (file <> "")
If file = yourFile Then
Debug.Print ("File: " & file & " found!")
Exit Do
End If
file = Dir
Loop
End Sub
Hope it helps

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.

Downloading a file into Application path

so i am using this code to download file
Dim exePath As String = Application.ExecutablePath()
My.Computer.Network.DownloadFile(TextBox2.Text, exePath & "/img.png")
where textbox2 contains url to .png file but i get this error
Note that test.exe is on Desktop.
You can do it in this way
My.Computer.Network.DownloadFile(TextBox2.Text, Environment.CurrentDirectory & "/img.png");
Environment.CurrentDirectory Will give you the current working directory.
I hope this will work for you.

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
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

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!