Search code examples
c#directorygetfiles

Cannot read any files or directories outside of my application directory


This is really annoying problem and it's going to drive me mad. I like to read information such like files, directories ect. but my app cannot find anything OUTSIDE its folder it runs in.

I'm using Visual Studio 2015 and developing Windows Universal apps.

This routine under works very well if I change the directory inside the folder my app run like "Assets" and any other folder. But outside of my app folder result is zero, not even any errors :-(

Ok, Here is the simple code, What I Do Wrong?

private void GetThem_Click(object sender, RoutedEventArgs e)
{
    string myDir = @"c:\mydir\";
    string[] files;
    files = Directory.GetFiles(myDir,"*.jpg");

    foreach (string stuff in files)
    {
        RESULT.Text = RESULT.Text + stuff + " , ";
    }
}

Solution

  • A quick search would have given you the answer : It is not possible to access the file system like a classic desktop app. The answer of @Rico Suter explain you what you can acces and how :

    • Directories which are declared in the manifest file (e.g. Documents, Pictures, Videos folder)
    • Directories and files which the user manually selected with the FileOpenPicker or FolderPicker
    • Files from the FutureAccessList or MostRecentlyUsedList
    • Files which are opened with a file extension association or via sharing

    Once a file is picked by the user, you can add it to MostRecentlyUsedList or FutureAccessList to use it again later using this snippet (C#) from MSDN :

    StorageFile file = await savePicker.PickSaveFileAsync();
    if (file != null)
    {
       // Add to MRU with metadata (For example, a string that represents the date)
       string mruToken = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "20120716");
    
       // Add to FA without metadata
       string faToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);  
    }
    

    Then store the retrieved token because you will need it to access the file using GetFileAsync(token)