![]() |
|
Copy file & Move to directory [HELP] - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.li/Forum-Visual-Basic-NET-Framework) +--- Thread: Copy file & Move to directory [HELP] (/Thread-Copy-file-Move-to-directory-HELP) Pages:
1
2
|
Copy file & Move to directory [HELP] - Skryptec - 10-15-2016 Hello everyone, I have been stuck on this part for ages and can't seem to fix it. Code: Dim AppData As String = GetFolderPath(SpecialFolder.ApplicationData + "\Fevino")
Dim FilePath As String = Application.ExecutablePath()
If System.IO.File.Exists(FilePath) = True Then
System.IO.File.Copy(FilePath, AppData, True)
End IfBasically what I want it do, is copy the application and then place it into AppData, but for some reason the code above gives me a error. "C:\Users\Gebruiker\AppData\Fevino" is a directory and not a file. Help is appreciated. RE: Copy file & Move to directory [HELP] - hybris.softwares - 10-15-2016 Use this snippet, .net framework doesn't have a built-in way of copying directories. Code: private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, true);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}RE: Copy file & Move to directory [HELP] - Bish0pQ - 10-15-2016 You should try using the "environment" class. Code: Dim AppData As String = Environment.SpecialFolder.ApplicationData & "\Fevino\"
Dim FilePath As String = Application.ExecutablePath()
If System.IO.File.Exists(FilePath) = True Then
System.IO.File.Copy(FilePath, AppData, True)
End IfAlso I would recommend importing the System.IO class since you wouldn't have to type System.IO before each thing you want to do with IO. I didn't know you were dutch as well. RE: Copy file & Move to directory [HELP] - Skryptec - 10-15-2016 (10-15-2016, 12:58 PM)Bish0pQ Wrote: You should try using the "environment" class. It can't find the directory now. ![]() (10-15-2016, 12:39 PM)hybris.softwares Wrote: Use this snippet, .net framework doesn't have a built-in way of copying directories. This is VB.NEt though. RE: Copy file & Move to directory [HELP] - hybris.softwares - 10-15-2016 vb .net version, Code: Private Shared Sub DirectoryCopy(sourceDirName As String, destDirName As String, copySubDirs As Boolean)
' Get the subdirectories for the specified directory.
Dim dir As New DirectoryInfo(sourceDirName)
If Not dir.Exists Then
Throw New DirectoryNotFoundException(Convert.ToString("Source directory does not exist or could not be found: ") & sourceDirName)
End If
Dim dirs As DirectoryInfo() = dir.GetDirectories()
' If the destination directory doesn't exist, create it.
If Not Directory.Exists(destDirName) Then
Directory.CreateDirectory(destDirName)
End If
' Get the files in the directory and copy them to the new location.
Dim files As FileInfo() = dir.GetFiles()
For Each file As FileInfo In files
Dim temppath As String = Path.Combine(destDirName, file.Name)
file.CopyTo(temppath, True)
Next
' If copying subdirectories, copy them and their contents to new location.
If copySubDirs Then
For Each subdir As DirectoryInfo In dirs
Dim temppath As String = Path.Combine(destDirName, subdir.Name)
DirectoryCopy(subdir.FullName, temppath, copySubDirs)
Next
End If
End SubRE: Copy file & Move to directory [HELP] - Bish0pQ - 10-15-2016 I'm sorry, the code needed some tweaking. Code: 'Declare the 2 variables
Dim AppData As String = GetFolderPath(SpecialFolder.ApplicationData)
Dim FilePath As String = My.Application.Info.DirectoryPath & "\ApplicationName.exe"
AppData = AppData & "\Fevino\"
'## Copy contents
If File.Exists(FilePath) = True Then
File.Copy(FilePath, AppData & "\application_name.exe")
Else
MessageBox.Show("File does not exist.")
End If
'## Show result
Process.Start(AppData)
MsgBox("File has been copied to: " & AppData & " from " & FilePath)I tested above code and it worked. I included the msgbox to see where it's copied from and where it's copied too. You can remove it though, just to show you where the files land. RE: Copy file & Move to directory [HELP] - Killpot - 10-16-2016 (10-15-2016, 12:10 PM)Skryptec Wrote: Hello everyone, the reason this is happening is because File.Copy does not preserve file name, you have to give it a name in the destination directory, you're basically trying to tell the system to copy your current exe and place it in the destination as a directory. RE: Copy file & Move to directory [HELP] - Skryptec - 10-17-2016 (10-16-2016, 03:38 AM)Killpot Wrote:(10-15-2016, 12:10 PM)Skryptec Wrote: Hello everyone, Yeah, I already fixed it, thanks though
RE: Copy file & Move to directory [HELP] - iaman - 10-25-2016 HELP!!! give the full code vb.net. Copy and Move To Folder RE: Copy file & Move to directory [HELP] - Bish0pQ - 10-25-2016 (10-25-2016, 10:59 AM)iaman Wrote: HELP!!! give the full code vb.net. Copy and Move To Folder Not sure what you mean by this, but if you have a question, I might be able to help you out. |