0

How to copy images and videos file asynchronous in c# WPF? I am already using this for copying txt files and its working, but if I use it for copying images or videos, the result does not open or crash. Any Idea what could be wrong? This is my code

private async void btnUpdate_Click(object sender, RoutedEventArgs e)
{
    string x2 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    DirectoryInfo dinfo = new DirectoryInfo(x2);
    FileInfo[] Files = dinfo.GetFiles("*.txt");
    int jml = 0;
    foreach (FileInfo file in Files)
    {
        string fileNoExtension = file.Name.Replace(".txt", "");


        string fileName2 = x2 + @"\Data\" + file.Name;



        using (StreamReader SourceReader = File.OpenText(@"Data\" + file.Name))
        {
            using (StreamWriter DestinationWriter = File.CreateText(x2 + @"\Data\" + file.Name))
            {
                await CopyFilesAsync(SourceReader, DestinationWriter);
            }
        }
        lbxHasil.Items.Add(file.Name);
        jml = jml + 1;
    }

    MessageBox.Show("Success add " + jml.ToString() + " New Songs!!!");
    btnUpdate.IsEnabled = false;
    lbxAwal.Items.Clear();
}
nofry27
  • 1
  • 1
  • You can use File.Copy function instead of using stream writer and reader – ITGenius Dec 14 '16 at 01:59
  • Possible duplicate of [Asynchronous File Copy/Move in C#](http://stackoverflow.com/questions/882686/asynchronous-file-copy-move-in-c-sharp) – Sphinxxx Dec 14 '16 at 02:12

1 Answers1

1

You need to use File.OpenRead and File.OpenWrite so it's treated as binary.

Or File.Create as JohnnyMopp said.

Alternatively if there's no additional processing File.Copy

It depends on what you need to do but your problems are related to treating it as text not binary.

LoztInSpace
  • 5,584
  • 1
  • 15
  • 27