c#: 打开文件夹并选中文件
一、常规方法
给定一个文件路径,打开文件夹并定位到文件,通常所用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#: 打开文件夹并选中文件的更多相关文章
- 使用ShellExecute打开文件夹并选中文件
原文链接: http://futurecode.is-programmer.com/posts/24780.html 假设在C:\目录下存在文件a.txt. 打开这个目录是ShellExecute的常 ...
- VC在windows中打开文件夹并选中文件
网上一位前辈高人的一段精髓代码让我眼前一亮…… ShellExecute(NULL, "open", "explorer.exe", "/select ...
- js打开所在文件夹并选中文件
该方法只支持IE. var wsh = new ActiveXObject("WSCript.shell"); var src = "/select," + ...
- [转]C#中调用资源管理器(Explorer.exe)打开指定文件夹 + 并选中指定文件 + 调用(系统默认的播放类)软件(如WMP)打开(播放歌曲等)文件
原文:http://www.crifan.com/csharp_call_explorer_to_open_destinate_folder_and_select_specific_file/ C#中 ...
- C# winform打开文件夹并选中指定文件
例如:打开“E:\Training”文件夹并选中“20131250.html”文件 System.Diagnostics.Process.Start("Explorer.exe", ...
- 使用ShellExecute打开目标文件所在文件夹并选中目标文件
转载:http://blog.csdn.net/chenlycly/article/details/7366364 转载:http://bbs.csdn.net/topics/50440550 She ...
- C#项目打开/保存文件夹/指定类型文件,获取路径
C#项目打开/保存文件夹/指定类型文件,获取路径 转:http://q1q2q363.xiaoxiang.blog.163.com/blog/static/1106963682011722424325 ...
- 使用C#选择文件夹、打开文件夹、选择文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口
原文:重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (2 ...
随机推荐
- electron安装到第一个实例
1.node.js下载,然后安装.下载地址:链接:http://pan.baidu.com/s/1o7TONhS 密码:fosa 2.cmd下输入:npm install electron-prebu ...
- js求时间差,两个日期月份差
var date1=new Date(); //开始时间 alert("aa"); var date2=new Date(); //结束时间 var date3=date2 ...
- Runloop原理
简单的说,runloop是一个事件循环的机制,同时能够保活线程.iOS中每个线程都对应一个runloop,主线程的runloop默认开启,其他线程的runloop默认关闭,线程与runloop是一一对 ...
- 3.有关于Python列表简述
一..title() [让所选择的列表元素的第一个字母大写] test = ['no1','No2','No3','No4'] book = "This My " + test[0 ...
- How to Pronounce the Days of the Week
How to Pronounce the Days of the Week Share Tweet Share Vocabulary study: how to pronounce the days ...
- Haskell语言练习
Monad inc n = Just (n + 1) add1 n = [n + 1] main = do print $ Nothing >> (Just 0) -- Nothing p ...
- Ajax 学习 第一篇
http请求: 1.请求的方法或动作 2.正在请求的url 3.请求头 4.请求体 及请求正文 典型例子 get.post区别 1.get 幂等 执行一次哪怕一万次影响相同 一般用于信息获取 ...
- A*寻路初探(转载)
启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省略大量无畏的搜索路径,提到了效率.在启发式搜索中,对位置的估价是十分重要 ...
- Swift中的的注释
1. Swift支持与OC中相同的 /**/ ./***/ 不同点Swift支持 /*/**/ 这样的注释 ,多行注释 2. 直接双杠注释 // 单行注释 3. 利用 //MARK: 返 ...
- windows安装AnyProxy 配合夜神模拟器抓包
AnyProxy是阿里巴巴基于 Node.js 开发的一款开源代理服务器.做为中间代理服务器,它可以收集所有经过它的http请求流量(包括https明文内容):它提供了友好的web界面,便于直观的查看 ...