打开文件所在位置 [英] Open file location

查看:71
本文介绍了打开文件所在位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows资源管理器中搜索文件时,右键单击搜索结果中的文件;有一个选项:打开文件位置".我想在我的C#WinForm中实现相同的功能.我是这样做的:

When searching a file in Windows Explorer and right-click a file from the search results; there is an option: "Open file location". I want to implement the same in my C# WinForm. I did this:

if (File.Exists(filePath)
{
    openFileDialog1.InitialDirectory = new FileInfo(filePath).DirectoryName;
    openFileDialog1.ShowDialog();
}

还有更好的方法吗?

推荐答案

如果openFileDialog_View

If openFileDialog_View is an OpenFileDialog then you'll just get a dialog prompting a user to open a file. I assume you want to actually open the location in explorer.

您可以这样做:

if (File.Exists(filePath))
{
    Process.Start("explorer.exe", filePath);
}


选择,文件explorer.exe采用这样的/select参数:


To select a file explorer.exe takes a /select argument like this:

explorer.exe /select, <filelist>

我是从SO帖子中获得的:在资源管理器中打开一个文件夹并选择一个文件

I got this from an SO post: Opening a folder in explorer and selecting a file

因此您的代码应为:

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

这篇关于打开文件所在位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆