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 ...
随机推荐
- Notepadd ++ PluginManager安装
下载地址https://github.com/bruderstein/nppPluginManager/releases 解压后有2个包plugins和updater 分别放入C:\Program F ...
- Dubbo的优化 --- 开发时使用
开发时的三个优化: 1.开发者在本地开发的时候启动Dubbo比较麻烦,所以采用直接连接的配置: 2.开发者本地开发时会打断点调试,会超过Dubbo默认的超时时间1s,所以需要全局设置超时时间: 3.开 ...
- dshow采集过程
捕捉静态图片常用的filter是Sample Graber filter,它的用法参考手册.然后将捕捉filter的静态PIN连接到Sample Grabber,再将Sample Grabber连接到 ...
- UI5-学习篇-1-Eclipse开发工具及环境搭建
最近研究SAP-UI5好几个月了,将相关学习经历及问题点做个记录. 1.先了解学习资料相关站点 SAP官网:https://www.sap.com/china/index.html SAP开发工具:h ...
- HTML一般标签
<title>无标题文档</title> </head> <body bgcolor="#33CC33" background=" ...
- java定时任务——间隔指定时间执行方法
摘要:运行 main 方法的时候开始进行定时任务, service.scheduleAtFixedTate(task,5,TimeUnit.SECONDS);方法为关键 此次任务就是 run() 方法 ...
- Python爬虫示例
#!/usr/bin/python #coding:utf8 import re import urllib def gethtml(url): page=urllib.urlopen(url) ht ...
- where后一个条件和多个条件的查询速度
如果记录中有两个都是 唯一标识的 ,那是都where and 还是只写一个比较快 ---- 一个快
- jquery Load方法的重要点
一个非常重要而且很容易忽视的问题就是:你是否load进了你必须load的元素,是否有的没有load进来,打开firebug查看一下
- openwrt手工配置pptpd
官方wiki:http://wiki.openwrt.org/doc/howto/vpn.server.pptpd#prerequisites 20190220更新:PPTP VPN协议已经被 IOS ...