目录
- 一、实现内容
- 1.1实现的功能
- 1.2实现的效果
- 二、实现操作
- 三、Windows 资源管理器参数说明
一、实现内容
1.1实现的功能
想要实现:
①打开指定的目录;
②打开指定的目录且选中指定文件;
③打开指定文件
1.2实现的效果

二、实现操作
/// <summary>
/// 打开目录
/// </summary>
/// <param name="folderPath">目录路径(比如:C:\Users\Administrator\)</param>
private static void OpenFolder(string folderPath)
{
if (string.IsNullOrEmpty(folderPath)) return;
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
psi.Arguments = folderPath;
process.StartInfo = psi;
try
{
process.Start();
}
catch (Exception ex)
{
throw ex;
}
finally
{
process?.Close();
}
}
/// <summary>
/// 打开目录且选中文件
/// </summary>
/// <param name="filePathAndName">文件的路径和名称(比如:C:\Users\Administrator\test.txt)</param>
private static void OpenFolderAndSelectedFile(string filePathAndName)
{
if (string.IsNullOrEmpty(filePathAndName)) return;
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
psi.Arguments = "/e,/select,"+filePathAndName;
process.StartInfo = psi;
//process.StartInfo.UseShellExecute = true;
try
{
process.Start();
}
catch (Exception ex)
{
throw ex;
}
finally
{
process?.Close();
}
}
/// <summary>
/// 打开文件
/// </summary>
/// <param name="filePathAndName">文件的路径和名称(比如:C:\Users\Administrator\test.txt)</param>
/// <param name="isWaitFileClose">是否等待文件关闭(true:表示等待)</param>
private static void OpenFile(string filePathAndName,bool isWaitFileClose=true)
{
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo(filePathAndName);
process.StartInfo = psi;
process.StartInfo.UseShellExecute = true;
try
{
process.Start();
//等待打开的程序关闭
if (isWaitFileClose)
{
process.WaitForExit();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
process?.Close();
}
}
三、Windows 资源管理器参数说明
Windows资源管理器参数的说明
| 序号 | 参数命令 | 说明 |
| 1 | Explorer /n | 此命令使用默认设置打开一个资源管理器窗口。显示的内容通常是安装 Windows 的驱动器的根目录 |
| 2 | Explorer /e | 此命令使用默认视图启动 Windows 资源管理器 |
| 3 | Explorer /e,C:\Windows | 此命令使用默认视图启动 Windows 资源管理器,并把焦点定位在 C:\Windows路径上 |
| 4 | Explorer /root, C:\Windows\Cursors | 此命令启动 Windows 资源管理器后焦点定位在 C:\Windows\Cursors folder路径上。此示例使用 C:\Windows\Cursors 作为 Windows 资源管理器的“根”目录 |
| 5 | Explorer /select, C:\Windows\Cursors\banana.ani | 此命令启动 Windows 资源管理器后选定“C:\Windows\Cursors\banana.ani”文件。 |
| 6 | Explorer /root, \\server\share, select, Program.exe | 此命令启动 Windows 资源管理器时以远程共享作为“根”文件夹,而且 Program.exe 文件将被选中 |
以上就是C#实现打开指定目录和指定文件的示例代码的详细内容,更多关于C#打开指定目录 文件的资料请关注其它相关文章!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)