C#: How to open Windows Explorer windows with a number of files selected

前端 未结 5 1183
花落未央
花落未央 2020-12-02 09:53

In the Library of Windows Media Player you can select one or more music files. You can then right-click and in their context menu choose Open File Location. This wi

5条回答
  •  半阙折子戏
    2020-12-02 11:00

    Disclaimer: I think VirtualBlackFox's answer is better than mine although it has less votes at present, so scroll down and read that one first :)

    Easy method (might not work on all platforms):

    Process.Start(String, String)
    

    First argument is the application, second argument is the command line parameters of the application..

    So for example:

    Process.Start("explorer.exe",
    "/select,Z:\Music\Thursday Blues\01. I wish it was friday.mp3")
    
    Process.Start("explorer.exe",
    "/select,Z:\Music\Counting Sheep\01. Sheep #1.mp3 /select,Z:\Music\Counting Sheep\02. Sheep #2.mp3")
    

    (I think you might need escaped quotes around the file paths if they have spaces).

    more info: http://msdn.microsoft.com/en-us/library/h6ak8zt5.aspx

    (compiled from several answers to this question)


    Harder method, but more likely to work, taken from this answer to another question:

    Use the shell function SHOpenFolderAndSelectItems

    Here is some sample code showing how to use the function in C/C++, without error checking:

    //Directory to open
    ITEMIDLIST *dir = ILCreateFromPath(_T("C:\\"));
    
    //Items in directory to select
    ITEMIDLIST *item1 = ILCreateFromPath(_T("C:\\Program Files\\"));
    ITEMIDLIST *item2 = ILCreateFromPath(_T("C:\\Windows\\"));
    const ITEMIDLIST* selection[] = {item1,item2};
    UINT count = sizeof(selection) / sizeof(ITEMIDLIST);
    
    //Perform selection
    SHOpenFolderAndSelectItems(dir, count, selection, 0);
    
    //Free resources
    ILFree(dir);
    ILFree(item1);
    ILFree(item2);
    

提交回复
热议问题