Reputation: 38152
Looking at the System.IO.File class, for example, I see the static Exists
method, but I don't see any ExistsAsync
counterpart. I suspect File.Exists
could block for quite some time if the file in question is, say, on a network share. Sure, I could always use Task.Run
, but that won't make use of I/O completion ports.
I could ask the same about many other static methods of the File
class.
Upvotes: 4
Views: 152
Reputation: 35869
I don't know why there isn't a File.ExistsAsync
method. It could be that "another process can potentially do something with the file in between the time you call the Exists
method and perform another operation on the file, such as Delete" and that catching exceptions is still required to ensure proper function of an application that accesses an existing file.
In any case, you could write your own.
public static async Task<bool> FileExistsAsync(string file)
{
return await Task.Factory.StartNew(() => File.Exists(file));
}
...which of course does not use IO Completion to get asynchronous IO...
Update: I think File is generally a convenience wrapper. You can do almost everything that File
offers using other APIs, which do offer asynchronous abilities. In the case of Exists
, it doesn't use anything that could use IO Completion ports, it just calls FindFirstFile and checks for error.
Upvotes: 2