none
How to open windows explorer and search from a desktop application? RRS feed

  • Question

  • I'm developing a windows desktop application and want to add a feature to open windows explorer and search a query in the computer from the application.

    Windows search protocol seems to satisfy my request. Below is my code snippet.

                    var query = "&query=" + HttpUtility.UrlEncode(rawQuery);
                    var location = string.Empty;
                    foreach (var drive in DriveInfo.GetDrives().Where(d => d.IsReady && d.DriveType.Equals(DriveType.Fixed)))
                    {
                        location += "&crumb=location:" + HttpUtility.UrlEncode(drive.Name);
                    }
                    var searchQuery = "search:displayname=Search computer" + query + location;
                    Process.Start(searchQuery);
    

    Above rawQuery is url encoded to escape special characters, like "&". 

    If above rawQuery is English, like microsoft, it works. The window explorer is open and searches "microsoft". But if above rawQuery is Chinese, like "微软", it searches å¾®è½¯ in windows explorer. It's bad.

    Does anybody have idea? Thanks in advance!

    Friday, April 7, 2017 10:15 AM

All replies

  • it is trying to convert Chinese characters to supported english. You might need to have your OS with regional settings of same language I believe.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Friday, April 7, 2017 10:18 AM
  • Try this:

       var query = "&query=" + rawQuery;

    or

       var query = "&query=" + Regex.Replace( rawQuery, @"\P{L}", m => HttpUtility.UrlEncode( m.Value ) );

    Friday, April 7, 2017 1:02 PM
  • Try this:

       var query = "&query=" + rawQuery;

    or

       var query = "&query=" + Regex.Replace( rawQuery, @"\P{L}", m => HttpUtility.UrlEncode( m.Value ) );

    Thanks for your reply!

    The first option cannot handle the query "&". If query is "&", nothing is passed into windows explorer search box.

    The second option can handle "&", English query, and Chinese query. However, it cannot handle the query with special characters like "€". If the query is "",  "€" is shown in windows explorer search box.

    What I want is exactly the same query in my application is shown in windows explorer search box. Do you have any idea?


    Saturday, April 8, 2017 1:16 PM
  • Check this idea:

    var query = "&query=" + Regex.Replace( rawQuery, @"[!*'();:@&=+$,/?#[\]]|\s", m => Uri.EscapeDataString( m.Value ) );

    Saturday, April 8, 2017 7:16 PM
  • Check this idea:

    var query = "&query=" + Regex.Replace( rawQuery, @"[!*'();:@&=+$,/?#[\]]|\s", m => Uri.EscapeDataString( m.Value ) );

    That seems to work. But I'm not sure if it works for all characters. I did not find any documentation saying which characters should be encoded/escaped.

    Do you know if there is a documentation explaining which character passed into windows search protocol needs to be encoded?

    Saturday, April 8, 2017 11:33 PM
  • Hi zhhzhang,

    >>Do you know if there is a documentation explaining which character passed into windows search protocol needs to be encoded?

    Please check the following document.

    https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Codepage_layout

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 18, 2017 7:58 AM
    Moderator