I have the below program that will move files from one directory to other. In sysnchronous way, it works fine.But I want to do it in the asynchronous way.
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The error says it all: you can’t
awaitsomething that isvoid. You can onlyawaitTasks and things that look similar toTasks (e.g.YieldAwaitable, that’s returned byTask.Yield()). But you most certainly can’tawaitvoid.There doesn’t seem to be a way to asynchronously move a file in .Net 4.5.
The best you can do is to either use something like
await Task.Run(() => fileinfo.MoveTo(target)), which will still block a thread, but not the current one (may be useful if you’re on the UI thread).Or, alternatively, you could copy the file yourself using
Streams (which can be asynchronous) and then delete the original.