Implement “Open Containing Folder” and highlight file

微笑、不失礼 提交于 2019-11-29 00:11:13

问题


This can be a handy functionality to have in a program that works with files/folders. It's easy enough to actually open the containing folder using:

System.Diagnostics.Process.Start( *path to folder* );

...but how do I go about actually selecting the target file within that parent folder? If I use the Process.Start method it actually attempts to open the file.


回答1:


According to Windows Explorer Command-Line Options you just need to start an explorer process with /select parameter.

For instance, 'explorer /select,c:\Windows' will open a window with c:\windows folder selected.

So simply Process.Start("explorer.exe", "/select," + filename) should be enough.




回答2:


Execute Explorer.exe with /select, "filename" command line argument

System.Diagnostics.Process.Start(
    "explorer.exe", 
    string.Format("/select, \"{0}\"", filename));



回答3:


Containing folder, Self directory is represented in many ways!!! Simple 2 ways are . and, .\. no idea what is the difference!.. :D From DOS and bat files... Start . or Start .\. (Y)

Try... these 2 works, but check whether this is the solution u expect!

System.Diagnostics.Process.Start("explorer.exe", @".\.");

Or

System.Diagnostics.Process.Start("explorer.exe", @".");

-

  • Sometimes the application is run from a temp directory or a different dir (eg: in Sandbox... or while being scanned by antivirus... etc. :)


来源:https://stackoverflow.com/questions/2829501/implement-open-containing-folder-and-highlight-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!