Jonas
Jonas

Reputation: 3283

How to implement an async/await version of the System.IO.Directory.CreateDirectory method?

I wonder how I can implement an async/await version of the Directory.CreateDirectory method in the System.IO namespace?

Upvotes: 1

Views: 584

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456947

CreateDirectory is an odd scenario. It would be ideal to have an asynchronous version built-in, particularly for opening/creating directories on a network drive.

Normally, you would be able to P/Invoke an asynchronous Win32 API if the BCL doesn't support async directly. However, in this case, the Win32 API does not actually expose asynchronous APIs for directories. So you'd have to go even lower - probably calling the file system driver directly (all device drivers support async I/O, so that would certainly work).

So, although it's not ideal, you're probably better off in this case just making a fake async method, i.e., wrapping the call in Task.Run.

On a side note, the Windows Store-style directory APIs are asynchronous. It's possible that they're calling beneath the Win32 API, but I actually rather doubt it - I'd expect they're implemented as fake asynchronous methods.

Upvotes: 5

Related Questions