Reputation: 67
I am using File.copy to copy some files. The problem is, it copies files asynchronus so it continues the program while still copying some files.
Now i got the tip to use filestream instead of file.copy.
The code i use now is: (example)
string instdir = (@"C:\test.dll");
string srcdir = @"C:\test1.dll";
File.Copy(srcdir, instdir, true);
The problem with this is, the "test.dll" value is read from a xml file (so there are much more files to copy) After the copy it should execute an .exe file, but it executes the file before everything is copied and that is giving the error.
Any tips?
I also have the next question:
I got a solution for above, this is a single file copy.
Now i also have this one:
public static void CopyFolder(DirectoryInfo source, DirectoryInfo install)
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFolder(dir, install.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(install.FullName, file.Name), true);
}
for multi file copy (copy everything in folder)
Any idea how to use it in that?
Thanks!
Upvotes: 1
Views: 3056
Reputation: 120508
So you can open two streams and copy the streams:
using(var src = File.OpenRead(@"srcPath"))
using(var dest = File.OpenWrite(@"destPath"))
{
src.CopyTo(dest); //blocks until finished
}
Upvotes: 4