An example code is shown below (source: http://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx) Here the method is defined as async because loading files on local drive may take time.
private async void Button_Click(object sender, RoutedEventArgs e)
{
string StartDirectory = @"c:\Users\exampleuser\start";
string EndDirectory = @"c:\Users\exampleuser\end";
foreach (string filename in Directory.EnumerateFiles(StartDirectory))
{
using (FileStream SourceStream = File.Open(filename, FileMode.Open))
{
using (FileStream DestinationStream = File.Create(EndDirectory + filename.Substring(filename.LastIndexOf('\\'))))
{
await SourceStream.CopyToAsync(DestinationStream);
}
}
}
}
Good resources on MSDN:
Asynchronous Programming with Async and Await (C# and Visual Basic)
http://msdn.microsoft.com/en-us/library/hh191443.aspx
Calling Synchronous Methods Asynchronously
http://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.110).aspx
async (C# Reference)
http://msdn.microsoft.com/en-us/library/hh156513.aspx
0 comments:
Post a Comment