[UWP][c#] How to override tostring method for "StorageFileQueryResult" class ?

kranthi kumar 206 Reputation points
2021-02-02T16:28:15.223+00:00

is there a way to override tostring method for "StorageFileQueryResult" class ?

Developer technologies | Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Yan Gu - MSFT 2,676 Reputation points
    2021-02-05T07:25:51.947+00:00

    Hello,

    Welcome to Microsoft Q&A.

    If you want to get a string from a StorageFileQueryResult to show the information for logging, then you could create a method which accepts a parameter whose type is StorageFileQueryResult, and return the string you needed from the StorageFileQueryResult instance.

    Or, if you want a better coupling, you could wrap a StorageFileQueryResult instance and a ToString() method in a single class.

    Here is a presentation for the second idea:

    public class MyStorageFileQueryResult  
    {  
        public StorageFileQueryResult storageFileQueryResult { get; set; }  
        public async Task<string> ToString()  
        {  
            //If your ToString() method is synchronous, you could create ToString() like this:  
            //public override string ToString()  
            string returnString = "StorageFile list:\n";  
            IReadOnlyList<StorageFile> fileList = await storageFileQueryResult.GetFilesAsync();  
    
            foreach (StorageFile file in fileList)  
            {  
                returnString += file.Name + "\n";  
            }  
    
            return returnString;  
        }  
    }  
    
    //A test  
    private async void Button_Click(object sender, RoutedEventArgs e)  
    {  
        List<string> fileTypeFilter = new List<string>();  
        fileTypeFilter.Add(".jpg");  
        fileTypeFilter.Add(".png");  
        fileTypeFilter.Add(".bmp");  
        fileTypeFilter.Add(".gif");  
        var queryOptions = new QueryOptions(CommonFileQuery.OrderByName, fileTypeFilter);  
    
        // Create query and retrieve files  
        MyStorageFileQueryResult query = new MyStorageFileQueryResult();  
        query.storageFileQueryResult= KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions);  
        string str =await query.ToString();  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.