Search code examples
c#xamarin.androidxamarinportable-class-librarycompact-framework2.0

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


I'm wondering if the Portable Class Library is even more restricted in functionality than the Compact Framework.

I'm trying to port a CF/Windows CE app (runs on a handheld device) to a Xamarin solution that will target Android, iOS, Windows Phone, and perhaps other things.

One of the problems I run into, though, is that this legacy code (which works under CF):

public static List<string> GetXMLFiles(string fileType, string startingDir)
{
    const string EXTENSION = ".XML";
    string dirName = startingDir;
    // call it like so: GetXMLFiles("ABC", "\\");  <= I think the double-whack is what I need for Windows CE device...am I right?
    var fileNames = new List<String>();
    try
    {
        foreach (string f in Directory.GetFiles(dirName))
        {
            string extension = Path.GetExtension(f);
            if (extension != null)
            {
                string ext = extension.ToUpper();
                string fileNameOnly = Path.GetFileNameWithoutExtension(f);
                if (fileNameOnly != null &&
                    ((ext.Equals(EXTENSION, StringComparison.OrdinalIgnoreCase)) &&
                     (fileNameOnly.Contains(fileType))))
                {
                    fileNames.Add(f);
                }
            }
        }
        foreach (string d in Directory.GetDirectories(dirName))
        {
            fileNames.AddRange(GetXMLFiles(fileType, d));
                // from Brad Rem's answer here: http://stackoverflow.com/questions/22186198/why-is-this-function-returning-nothing-although-there-is-a-match/22186351?noredirect=1#22186351
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return fileNames;
}

...won't compile in the Xamarin/CPL solution. I get, "The name 'Directory' does not exist in the current context" and right-clicking that word does not afford a "resolve" option.

Is there a way to get PCL to recognize "Directory" or must I completely rewrite the code? If the latter, does anybody have any suggestions on what to do/use in its stead?

Relatedly, is there an URL that will show me what is [not] available in PCL and/or a site that will show how much of a provided block of code is "PCL-ready"?

UPDATE

The first image in this article is very illuminating. Later on, it specifically talks about "Directory" not being available in the PCL scenario.

UPDATE 2

I downloaded the PCLStorage package referenced by Daniel Plaisted below to allow me to access the file system within a PCL project.

Using the sample code at the start of the download page [http://pclstorage.codeplex.com/] as a starting point, I've gotten this far:

public async Task<List<string>> GetXMLFiles(string fileType, string startingDir)
{
    const string EXTENSION = ".XML";

    IFolder rootFolder = FileSystem.Current.LocalStorage;
    IFolder folder = await rootFolder.GetFolderAsync(startingDir, CreationCollisionOption.OpenIfExists); //CreateFolderAsync(startingDir, CreationCollisionOption.OpenIfExists);
    List<string> fileNames = await folder.GetFilesAsync(EXTENSION);
    return fileNames;
}

...but "EXTENSION" as the arg to GetFilesAsync() is not right. I get with this, "Argument 1: cannot convert from 'string' to 'System.Threading.CancellationToken'"

So what need I do to get all the *.XML files the folder?

UPDATE 3

This compiles, but I'm not at all sure it's the right way to do it, besides the fact that it simply gets all the files from the folder, rather than just those that match "*.XML":

public async Task<List<IFile>> GetXMLFiles(string fileType, string startingDir)
{
    const string EXTENSION = ".XML";

    IFolder rootFolder = FileSystem.Current.LocalStorage;
    IFolder folder = await rootFolder.GetFolderAsync(startingDir, System.Threading.CancellationToken.None);
    IList<PCLStorage.IFile> fileNames = await folder.GetFilesAsync(System.Threading.CancellationToken.None);

    return fileNames.ToList();
}

Solution

  • 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