Login Register






Copy file & Move to directory [HELP] filter_list
Author
Message
Copy file & Move to directory [HELP] #1
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 If

Basically 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.
[Image: oAqtc2l.png]

Reply

RE: Copy file & Move to directory [HELP] #2
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); } } }

Reply

RE: Copy file & Move to directory [HELP] #3
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 If

Also 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.
~~ Might be back? ~~

Reply

RE: Copy file & Move to directory [HELP] #4
(10-15-2016, 12:58 PM)Bish0pQ Wrote: 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 If

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

It can't find the directory now.
[Image: 0b435343e04741c1b2a252f293edd31e.png]

(10-15-2016, 12:39 PM)hybris.softwares Wrote: 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);                }            }        }

This is VB.NEt though.
(This post was last modified: 10-15-2016, 02:56 PM by Skryptec.)
[Image: oAqtc2l.png]

Reply

RE: Copy file & Move to directory [HELP] #5
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 Sub

Reply

RE: Copy file & Move to directory [HELP] #6
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.
~~ Might be back? ~~

Reply

RE: Copy file & Move to directory [HELP] #7
(10-15-2016, 12:10 PM)Skryptec Wrote: 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 If

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

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.


[+] 1 user Likes Killpot's post
Reply

RE: Copy file & Move to directory [HELP] #8
(10-16-2016, 03:38 AM)Killpot Wrote:
(10-15-2016, 12:10 PM)Skryptec Wrote: 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 If

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

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.

Yeah, I already fixed it, thanks though Smile
[Image: oAqtc2l.png]

Reply

RE: Copy file & Move to directory [HELP] #9
HELP!!! give the full code vb.net. Copy and Move To Folder

Reply

RE: Copy file & Move to directory [HELP] #10
(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.
~~ Might be back? ~~

Reply