Is it possible to switch between System.IO and the StorageFile methods depending on if the user is running Windows 8 or not, without having to create two separate projects? I know I can use System.IO in Windows 8, but I’d like to take advantage of the built in async methods if it’s possible (i.e. user is on Win8).
I don’t have a Windows 7 machine to test on at the moment, but I’m concerned that missing library errors may be thrown on the program’s startup. Is there a way to do this?
StorageFile class is not a part of .NET 4.5, it’s a part of Windows Runtime. Such classes can easily be recognized by their root namespace
Windows. Windows Runtime API is available only on Windows 8 and is primarily designed for use inWindows Store apps. Although I haven’t tried it myself they can be used in desktop applications as well. Still I’m not sure how good that works for file IO classes since Windows store apps run in a sandboxed environment and have only limited access to file system based on the capabilities declared in their manifest.For file IO in a desktop application I’d stick with .NET classes in
System.IOnamespace. In .NET 4.5 there are several async methods available as well, mostly in Stream, StreamReader and StreamWriter classes and can be used for reading from and writing to files with the async/await paradigm. They are not bound to Windows 8 only, but they do require .NET 4.5 to be installed which excludes Windows XP.Unfortunately there’s no async method equivalent to
StorageFile.GetBasicPropertiesAsyncin .NET 4.5 that I know of. If you really need to make these calls async as well, you can always wrap the synchronous calls into your own async method which you can call from the UI thread.