一、常规方法

给定一个文件路径,打开文件夹并定位到文件,通常所用shell命令为:explorer.exe /select,filepath。

c#以进程启动之为:

if (File.Exists(fileName))
{
Process.Start("explorer", "/select,\"" + fileName + "\"");
}

此命令对于一般文件名是适用的,是最为简便的方法。

但项目中碰到特殊文件名,explorer.exe就不认了,它找不到,它默认跳到我的文档目录。

比如下列文件名:

它在c#代码中,unicode字符实为:

而在命令行窗口中,以explorer /select,执行之,则又如下:

结果它自然是找不到的,所以它打开了我的文档目录。

二、SHOpenFolderAndSelectItems API

万能的stackoverflow!

这个纯粹技术的网站,从上受益良多。曾在上面扯过淡,立马被警告,从此收敛正容。

蛛丝蚂迹,找到了SHOpenFolderAndSelectItems这个API,解决问题:

    /// <summary>
/// 打开路径并定位文件...对于@"h:\Bleacher Report - Hardaway with the safe call ??.mp4"这样的,explorer.exe /select,d:xxx不认,用API整它
/// </summary>
/// <param name="filePath">文件绝对路径</param>
[DllImport("shell32.dll", ExactSpelling = true)]
private static extern void ILFree(IntPtr pidlList); [DllImport("shell32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern IntPtr ILCreateFromPathW(string pszPath); [DllImport("shell32.dll", ExactSpelling = true)]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlList, uint cild, IntPtr children, uint dwFlags); public static void ExplorerFile(string filePath)
{
if (!File.Exists(filePath) && !Directory.Exists(filePath))
return; if (Directory.Exists(filePath))
Process.Start(@"explorer.exe", "/select,\"" + filePath + "\"");
else
{
IntPtr pidlList = ILCreateFromPathW(filePath);
if (pidlList != IntPtr.Zero)
{
try
{
Marshal.ThrowExceptionForHR(SHOpenFolderAndSelectItems(pidlList, , IntPtr.Zero, ));
}
finally
{
ILFree(pidlList);
}
}
}
}

试用一下:

非常完美。而且如果其目录已打开,它会已打开的目录中选择而不会新开文件夹,更人性化。

c#: 打开文件夹并选中文件的更多相关文章

  1. 使用ShellExecute打开文件夹并选中文件

    原文链接: http://futurecode.is-programmer.com/posts/24780.html 假设在C:\目录下存在文件a.txt. 打开这个目录是ShellExecute的常 ...

  2. VC在windows中打开文件夹并选中文件

    网上一位前辈高人的一段精髓代码让我眼前一亮…… ShellExecute(NULL, "open", "explorer.exe", "/select ...

  3. js打开所在文件夹并选中文件

    该方法只支持IE. var wsh = new ActiveXObject("WSCript.shell");  var src = "/select," + ...

  4. [转]C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件

    原文:http://www.crifan.com/csharp_call_explorer_to_open_destinate_folder_and_select_specific_file/ C#中 ...

  5. C# winform打开文件夹并选中指定文件

    例如:打开“E:\Training”文件夹并选中“20131250.html”文件 System.Diagnostics.Process.Start("Explorer.exe", ...

  6. 使用ShellExecute打开目标文件所在文件夹并选中目标文件

    转载:http://blog.csdn.net/chenlycly/article/details/7366364 转载:http://bbs.csdn.net/topics/50440550 She ...

  7. C#项目打开/保存文件夹/指定类型文件,获取路径

    C#项目打开/保存文件夹/指定类型文件,获取路径 转:http://q1q2q363.xiaoxiang.blog.163.com/blog/static/1106963682011722424325 ...

  8. 使用C#选择文件夹、打开文件夹、选择文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. 重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

    原文:重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (2 ...

随机推荐

  1. java 浅克隆(浅复制)和深克隆(深复制)

    http://www.voidcn.com/blog/u011380813/article/p-6161450.html https://gold.xitu.io/entry/570d89651ea4 ...

  2. 【Noip模拟 20161005】友好城市

    问题描述 小ww生活在美丽的ZZ国.ZZ国是一个有nn个城市的大国,城市之间有mm条单向公路(连 接城市ii.jj的公路只能从ii连到jj).城市ii.jj是友好城市当且仅当从城市ii能到达城市jj并 ...

  3. 1.ossutil初步使用

    ossutil对应的阿里云参考文档链接地址: https://help.aliyun.com/document_detail/50452.html?spm=a2c4g.11186623.6.1355. ...

  4. 在WINDOWS上开发IOS应用的方法

    1,虚拟机上安装MAC系统,里面装XCODE 2,WINDOWS上安装code blocks,这个IDE可以直接在WIN上编译XCODE工程: 最新的IOS在windows环境下编译环境搭建记录

  5. jenkins系列_使用scp命令进行远程文件复制遇到的坑

    转自:https://blog.csdn.net/kingboyworld/article/details/78905553 一.场景介绍 项目为微服务项目,使用jenkins进行统一部署.基本思路是 ...

  6. 关于openwrt使用web升级提示固件版本不对的处理方法

    参考资料:https://blog.csdn.net/caoshunxin01/article/details/79355602 当openwrt使用web升级提示固件版本不对: The upload ...

  7. Todolist组件

    一.什么是组件? 组件是可复用的 Vue 实例.是页面上的某一部分. 大型项目可以拆分成很多小组件. 二.如何定义(创建)组件? 全局组件:通过Vue.component方法创建的组件是全局组件.其中 ...

  8. shell-保留文件系统下剩余指定数目的文件

       path_backup=/mnt/fifth/backup/shellbackup  path_delete=/mnt/fifth/tmp/rubbish/  limit_num=15    f ...

  9. app.route()

    [app.route()] 可使用 app.route() 创建路由路径的链式路由句柄.由于路径在一个地方指定,这样做有助于创建模块化的路由,而且减少了代码冗余和拼写错误.请参考 Router() 文 ...

  10. BOM 对象--location、navigator、screen、history

    1.location 对象 location提供了与当前窗口中加载的文档有关的信息,还有一些导航功能.需要注意的是,window.location 和 document.location 引用的是同一 ...