locked
Getting file information from a folder RRS feed

  • Question

  • How Can I get the information of all files(images) in the same folder?

    What I have in mind, is to show one of the images randomly and use the file name as a key. The user will visualize the image, and in a textbox write the corresponding key-word, wich will already be the name of the file.

    I'm new with both C# and Visual Studio, can somebody give me some help? I appreciate it.

    Monday, May 13, 2013 5:43 PM

Answers

  • and also to access such type of files from the appxmanifest of your app

    Only add the file types to your appxmanifest if you want the user to be able to choose your app as the default handler for those types. It is not necessary for browsing and loading files from within the app.

    --Rob

    Tuesday, May 14, 2013 7:22 PM
    Moderator

All replies

  • var files = await folder.GetFilesAsync();
    where folder is an instance of StorageFolder

    http://lunarfrog.com/blog/

    Monday, May 13, 2013 11:57 PM
  • Also take a look at here

    http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597

    thank you

    zkar

    Tuesday, May 14, 2013 5:09 AM
  • Look at this sample to know how to  get the Properties of a file, so that you can filter out image files from all the files in a folder(using the FileType property of a File)

    http://code.msdn.microsoft.com/windowsapps/File-access-sample-d723e597


    pradeep aj If it answers, then please Mark it as Answer


    • Edited by Pradeep AJ Tuesday, May 14, 2013 5:14 AM
    Tuesday, May 14, 2013 5:10 AM
  • This code will help you

     private async void Button_Click_1(object sender, RoutedEventArgs e)
            { 
                Windows.Storage.StorageFolder folder = await KnownFolders.PicturesLibrary.GetFolderAsync("New Folder");
                var files = await folder.GetFilesAsync();
                foreach (StorageFile file in files)
                {
                    if(file.FileType==".jpg")
                    t.Text += file.Name;    
                }
            }

    GetFileAsync() -gets all files from the directory(ie. the "New Folder" inside Pictures Library)


    pradeep aj If it answers, then please Mark it as Answer

    Tuesday, May 14, 2013 6:01 AM
  • Remember to get permission to access Pictures Library and also to access such type of files from the appxmanifest of your app

    pradeep aj If it answers, then please Mark it as Answer

    Tuesday, May 14, 2013 6:03 AM
  • Thank you very much!

    Just a few more things, is it possible to make the app load those files as soon as it starts to run?

    Can I implement this function within a navigation button?

    And I'll put the files in the Assets Directory, would it go like this?

    ...

    Windows.Storage.StorageFolder folder = await GetFolderAsync("Assets/New Folder");

    ...

    Tuesday, May 14, 2013 1:39 PM
  • Yes you can do it, when the app starts by putting the code in

     protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }
    

    Yes, you can also do this while navigating to other page by putting the code in

     protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
            }
    

    No, you should access your Assets directory like this

     var folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets");
                var subfolder = await folder.GetFolderAsync("NewFolder1");
                var files = await subfolder.GetFilesAsync();
                foreach (StorageFile file in files)
                {
                    if(file.FileType==".png")
                    t.Text += file.Name;    
                }


    pradeep aj If it answers, then please Mark it as Answer

    Tuesday, May 14, 2013 3:17 PM
  • That was more than helpful, thank you very much!!
    Tuesday, May 14, 2013 6:31 PM
  • Don't load all the files and then iterate through to get rid of the ones you don't want. Instead use file queries (StorageFolder.CreateFileQueryWithOptions, etc. ) to get just the files you are interested in and to pre-load properties the app will need.

    These API are demonstrated in the Programmatic file search sample and Folder enumeration samples.

    --Rob

    • Proposed as answer by Pradeep AJ Wednesday, May 15, 2013 2:52 AM
    Tuesday, May 14, 2013 7:16 PM
    Moderator
  • and also to access such type of files from the appxmanifest of your app

    Only add the file types to your appxmanifest if you want the user to be able to choose your app as the default handler for those types. It is not necessary for browsing and loading files from within the app.

    --Rob

    Tuesday, May 14, 2013 7:22 PM
    Moderator
  • Thanks for your suggestion sir.  t273p, better follow Rob Caplan's answer, which is more efficient  than what i suggested.


    pradeep aj

    • Edited by Pradeep AJ Wednesday, May 15, 2013 2:55 AM
    Wednesday, May 15, 2013 2:55 AM
  • I ran into another problem.

    I'm not being able to access the variable files in other parts of the code.

    How can I load all the files just as the app starts to run?

    And how to make the varialbles, in wich I'll put the files, accessible at any part of the app?

    Wednesday, May 15, 2013 6:41 PM
  • You can take a look at How to track recently used files and folders article

    http://lunarfrog.com/blog/

    Wednesday, May 15, 2013 7:15 PM
  • But I can't seem to use a querie to randomize the files and then get the one file I need. Actually I'll need all of them, but one at a time, randomly.
    Wednesday, May 15, 2013 7:38 PM
  • Can I program a query that will get only one file randomly?
    Wednesday, May 15, 2013 7:44 PM