How to search StorageFiles

andy123456

Senior Member
Feb 20, 2009
66
0
0
I need a way to search in StorageFiles with dynamically pattern, which comes from a TextBox. The directive "Windows.Storage.Search" doesnt exist in windows phone 8.1 runtime, as i saw. Now my question is, how can i do this in alternative way?
 

snickler

Retired Forum Moderator / Inactive Recognized Deve
Aug 17, 2010
1,320
1,130
0
Dub V
www.sinclairinat0r.com
The only way to do it with WP 8.1 since Microsoft ALWAYS fails to implement the important things are to query using LINQ.

Ex:
Code:
var result = (await Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName)).
                   Where(x => x.Name.
                              Contains(x => txtBox.Text));
That's about all you can do pretty much. (Thanks Microsoft).
 
  • Like
Reactions: andy123456

andy123456

Senior Member
Feb 20, 2009
66
0
0
Thank you for the example. But it wont work for me, it shows me the following error(s):

Code:
A local variable named 'x' cannot be declared in this scope because it would give a different meaning to 'x', which is already used in a 'parent or current' scope to denote something else
and

Code:
Cannot convert lambda expression to type 'string' because it is not a delegate type
Thats really odd from Microsoft, that they havent implementet the search function like in WinRT (Windows Store App).
 

Tonchi91

Member
Nov 24, 2013
21
0
0
The first error is pretty simple. You already have the variable named "x" and it would be very bad if compiler didn't give you that error.
Change the name of the variable to something else that you don't use in that scope and it will work.

And for second problem, try this one:

Code:
private List<string> Result()
        {
            var result = ((List<Windows.Storage.Search.CommonFileQuery>)Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName)).Where(x => x.ToString().Contains(txtBox.Text));
            return result as List<string>;
        }

        private async Task<List<string>> ResultAsync()
        {
            return await Task.Run(() => Result()).ConfigureAwait(continueOnCapturedContext: false);
        }
You should call ResultAsync method and get the result in this way:

Code:
List<string> myList = ResultAsync().Result;
 

snickler

Retired Forum Moderator / Inactive Recognized Deve
Aug 17, 2010
1,320
1,130
0
Dub V
www.sinclairinat0r.com
That's not going to work. You can't cast a StorageFile as a string.

To fix my code (simple lambda typo)

Code:
var result = (await Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName)).
                   Where(x => x.Name.
                              Contains(txtBox.Text));
if(result.Any())
{

// Do shtuff

}
Also, you should never access the .Result of an async task because you never know if it completed yet.
 
Last edited:

andy123456

Senior Member
Feb 20, 2009
66
0
0
Ok, first error is done, but the second error is still here

Code:
Cannot convert lambda expression to type 'string' because it is not a delegate type
 

Tonchi91

Member
Nov 24, 2013
21
0
0
You are missing the point of the TAP (Task Async Pattern).
Both main thread and async method will be in execution in the same time. When the async method finish his work, main thread will stop and catch the result trough the Result property.

TAP is the recommended way of asynchronous programming in C#. The only thing with TAP is to use ConfigureAwait method in non-console type of apps to avoid deadlock.

Sooner or later you will get the result from TAP method. Nothing will get in the conflict with the main thread.
 

andy123456

Senior Member
Feb 20, 2009
66
0
0
Ok the errors are gone, but the debugger show me the following exception:

Code:
Value does not fall within the expected range
Is this search method case-sensitive? I tried with an exact input in the TextBox.
 

andy123456

Senior Member
Feb 20, 2009
66
0
0
its actually only for testing, so i added your code to a button (asnyc) and will show the output in a textBlock.

Code:
private async void buttonTest_Click(object sender, RoutedEventArgs e)
        {
            //Result();
            var result = (await Windows.Storage.KnownFolders.CameraRoll.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName)).
                   Where(x => x.Name.
                              Contains(textBox_test.Text));
            if (result.Any())
            {
                // Do shtuff
                textBlock_test.Text = result.ToString();
            }
        }
The error is coming from here

Code:
var result = (await Windows.Storage.KnownFolders.CameraRoll.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName))
 

snickler

Retired Forum Moderator / Inactive Recognized Deve
Aug 17, 2010
1,320
1,130
0
Dub V
www.sinclairinat0r.com
its actually only for testing, so i added your code to a button (asnyc) and will show the output in a textBlock.

Code:
private async void buttonTest_Click(object sender, RoutedEventArgs e)
        {
            //Result();
            var result = (await Windows.Storage.KnownFolders.CameraRoll.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName)).
                   Where(x => x.Name.
                              Contains(textBox_test.Text));
            if (result.Any())
            {
                // Do shtuff
                textBlock_test.Text = result.ToString();
            }
        }
The error is coming from here

Code:
var result = (await Windows.Storage.KnownFolders.CameraRoll.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName))

Oh Camera Roll.. You MIGHT need to have the capability to view the camera roll enabled. I forget what it's called, but you need a specific cap in order to view from there. Also, I would try to see if you can use a generic folder instead.

I would try Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync() as your method after the await just to test whether you can read correctly.
 
  • Like
Reactions: andy123456

andy123456

Senior Member
Feb 20, 2009
66
0
0
Yes but in wp8.1 runtime app, there arent caps anymore. The capability for access to the pictures is simply calles pictures library and is enabled. I have tested it as you said, but it gives me the same exception.
 

GoodDayToDie

Inactive Recognized Developer
Jan 20, 2011
6,066
2,930
0
Seattle
A quick tip: another way to do this is to use the Win32 C runtime API. You can, for example, use the FindFirst/NextFile functions (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx) which support searches using wildcards (* and ? characters in the first parameter). These functions are wrapped in my NativeLibraries classes, but are also just publicly available for third0party developers to call from their own C++ DLLs.

Alternatively, you can use the .NET System.IO.Directory class, which has functions like EnumerateFiles(String path, String searchPattern). This is probably the better way to do it, actually.

Of course, if you want these operations to not block the current thread, you'll need to explicitly put them in their own thread or async function.
EDIT: This also assumes you have read access to the relevant directories. You application data directory works fine, for example (you can get its path from the relevant StorageFolder object). Other directories that can be accessed via WinRT functions may go through a broker function instead of being directly readable.
 
Last edited:

andy123456

Senior Member
Feb 20, 2009
66
0
0
The point is, that i have an array with filenames. Now i need the StorageFile files which contains these filenames. My idea was to search for these files and return the files as StorageFile, so i can work with these. Or is there a simpler / another way?
 

andy123456

Senior Member
Feb 20, 2009
66
0
0
Thank you, i have already done this and its working. But how can i compare the Files to read, with already read files and take only the not yet read files?
 
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone