What need I do to get this code to work in a Portable Class Library?

寵の児 提交于 2019-11-29 16:26:50

Windows Phone applications do not use the file system of the operating system and are restricted to using isolated storage to persist and access files, so this namespace does not provide any additional functionality.

http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.io%28v=vs.105%29.aspx

Since in a PCL I was unable to get a StreamWriter from a string (it required a stream), I created a simple interface to get some of the data from the platform implementation. You can also do this with DirectoryInfo and FileInfo.

https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Text/IStreamLocator.cs

The implementation is really simple as well, only needs one single compiler flag for WP8:

https://github.com/sami1971/SimplyMobile/blob/master/WP8/SimplyMobile.Text.Platform/StreamLocator.cs

Recursively search for *.XML files:

    private static void PrintDirectory(IStreamLocator locator, string dir)
    {
        foreach (var file in locator.GetFileNames(dir, "*.XML"))
        {
            System.Diagnostics.Debug.WriteLine(file);
        }

        foreach (var di in locator.GetFolderNames(dir, "*"))
        {
            PrintDirectory(locator, di);
        }
    }

Xamarin has a scanner which will give you a rough idea of the portability of your code: http://scan.xamarin.com/

For some guidance on how to deal with non-portable APIs from PCLs, see my blog post: How to Make Portable Class Libraries Work for You

For file IO in particular, you can try my PCL Storage library.

Another option is to use Shim if all your platforms are supported by it.

API coverage for file operations isn't exhaustive, but it gets you a long way. As a bonus, it also gives you access to a bunch of other stuff.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!