一、常规方法

给定一个文件路径,打开文件夹并定位到文件,通常所用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. FB4.6项目迁移到4.7时 embed报错问题

    问题: 从FB4.6或更早版本移植到4.7的项目Embed标签,比如 [Embed(source="assets/BtnPlay.png")]   ,会报错 解决 方案: 4.7E ...

  2. UI5-Fiori初学者导航

    正文 你是UI5和Fiori的新手?来对地方了. 对我来说,今年是不得不“跟上时代”去提升自己ABAP世界以外的技术技能的困难的一年.幸运的是,有很多可免费获得的信息和课程可以帮你实现这个跳跃.不要等 ...

  3. css样式表1

    1内联样式表 和html联合使用,控制精确,但是可重用性差,冗余多. <p style="font-size:14px;"></p> <div sty ...

  4. jeecg使用小结

    1.上传word模板时报“java.lang.UnsatisfiedLinkError: no jacob-1.17-M2-x64 in java.library.path”异常 原因:jdk下没有找 ...

  5. Pronunciation – The Definitive Guide to the Top 100 Words in American English

    Pronunciation – The Definitive Guide to the Top 100 Words in American English Share Tweet Share Tagg ...

  6. JSON数据的解析和生成(Swift)

    Codable public typealias Codable = Decodable & Encodable public protocol Decodable {} public pro ...

  7. python技巧 显示对象的所有属性

    python技巧 显示对象的所有属性for attr in dir(ad):... print attr+":"+str(getattr(ad,attr))

  8. linux下搭建生成HLS所需的.ts和.m3u8文件

    要想利用HLS来实现视频的在线播放,就得需要将一个完整的视频文件切割成多个ts视频流,然后利用m3u8的索引文件来播放. 在Mac下,苹果提供了streamingTools的工具,里面有mediafi ...

  9. eclipse maven项目 热部署

    热部署:本地项目一键发布到远程服务器中 热部署步骤: 1. 在tomat/conf/tomcat-users.xml添加 <role rolename="manager-gui&quo ...

  10. 吴裕雄 oracle 函数、触发器和包编程