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 ...
随机推荐
- js删除dom节点时候索引出错问题
我们知道删除一个dom节点的时候索引就会发生了改变,甚至是错误,就算jq的ecah也无能为力,所以我们只能自己写个功能了 直接上代码把,不多说 <!DOCTYPE html> <ht ...
- js的非空校验
利用TagName获取元素名称,进行批量非空校验 var input = document.getElementsByTagName("input"); for (var i=0; ...
- 重建redo文件
需求背景 由于前期安装oracle时redo文件大小或者路径规划不合理需要进行修改,以便满足性能测试要求.redo文件规划大小建议与生产环境一致. 重做日志相关数据字典 1.v$log 记录数据库中 ...
- android填满手机内存的方法
1. 进行临界测试,手机盘空间存满的条件下应用会有何表现:通常手动添加大文件但是还是不够,通过如下 2. 使用adb命令完成:通过如下 adb 命令在 /mnt/sdcard/ 目录下产生一个名为 b ...
- 处理TypeError: Converting circular structure to JSON
// Demo: Circular reference var o = {}; o.o = o; // Note: cache should not be re-used by repeated ca ...
- c语言基本数据类型及存储方式
; ; ; ; ; ; ; ; float a9 = 109.23; float a10 = 111.23; double a11 = 113.113; double a12 = 115.113; c ...
- C# 图像处理:复制屏幕到内存中,拷屏操作
/// <summary> /// 复制屏幕到内存中 /// </summary> /// <returns>返回内存流</returns> publi ...
- C#中Graphics的画图代码【转】
我要写多几个字上去 string str = "Baidu"; //写什么字? Font font = Font("宋体",30f); //字是什么样子的? B ...
- windows7 IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET
IIS7报错:如果要使用托管的处理程序,请安装 ASP.NET windows7,部署在本地的IIS7里以后,结果不能访问承载SL的.aspx页面,而如果用.html承载则可以访问. 亲测可用修复办法 ...
- Docker 批量启动
批量配置IP for i in `docker ps -a|awk 'NR>1 {print $NF}'`;do IP=`echo $i|awk -F_ '{print "192.16 ...