1

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!

  • Look at [this](http://stackoverflow.com/questions/10982104/wait-until-file-is-completely-written) SO question – Jeroen Heier Jun 16 '16 at 10:10
  • This is becoming a chameleon question. Please don't evolve your question. If you have a new question, ask a new question. Don't alter a well defined question to ask another question (and another after that?). – spender Jun 16 '16 at 15:06

1 Answers1

4

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
}
spender
  • 117,338
  • 33
  • 229
  • 351