本文最后更新于 986 天前,其中的信息可能已经有所发展或是发生改变。
我们在执行DOS命令中已经初略讲过如何使用[code]Process[/code]类了,这篇文中我们就主要讲讲如何用此类打开文件资源管理器。
[v_blue]注意:使用[code]Process[/code]类需引用[code]System.Diagnostics[/code][/v_blue]
1.打开资源管理器
Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "explorer.exe");
2.打开文件资源管理器,浏览指定文件夹
Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\explorer.exe","C:\\"/*要打开的文件夹路径*/);
3.打开文件资源管理器,选中指定文件
【引用】
文件资源管理器的参数如下
/n
为默认选择内容打开一个新的单窗格窗口。默认选择内容通常是安装 Windows 的驱动器的根目录。
/e
使用默认视图启动 Windows 资源管理器。
/e,<object>
使用默认视图启动 Windows 资源管理器并把焦点定位在指定文件夹上。
/root,<object>
打开指定对象的一个窗口视图。
/select,<object>
打开一个窗口视图,指定的文件夹、文件或程序被选中。
显然,第5种是我们想要的。所以代码如下
Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\explorer.exe", "/select, C:\\unknow.log"/*要打开的文件路径*/);
实例应用
输入文件名或文件夹路径并打开&处理
源代码
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Explorer { class Program { static void Main(string[] args) { string in_str = ""; if (args.Length == 0) { Console.Beep(4000, 500); Console.Title = "文件浏览"; Console.WriteLine("[ 文件浏览 Ver=1.3 Author=C#之家 Website=https://www.chsarphome.xyz ]"); Console.Write("输入文件或文件夹路径:"); in_str = Console.ReadLine(); } else { in_str = args[0]; } if (!Path.HasExtension(in_str)) { Console.WriteLine(); Console.WriteLine("{0}是一个文件夹路径,请选择操作:", in_str); Console.WriteLine(" 【1】直接打开"); Console.WriteLine(" 【2】在此处浏览处理文件"); Console.WriteLine(" 【E】退出"); switch (Console.ReadLine()) { case "1": //打开文件资源管理器,浏览指定文件夹 Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\explorer.exe", in_str); break; case "2": Console.WriteLine(); DirectoryInfo directory = new DirectoryInfo(in_str); Console.WriteLine("【文件夹信息】创建时间:{0}", directory.CreationTime.ToLocalTime().ToString()); int p = 1; foreach(DirectoryInfo a in directory.GetDirectories()) { Console.WriteLine(" 【{0:00}】{1}", p, a.FullName.Substring(a.FullName.LastIndexOf("\\"))); p++; } int c = p; foreach (FileInfo a in directory.GetFiles()) { Console.WriteLine(" 【{0:00}】{1}", p, a.FullName.Substring(a.FullName.LastIndexOf("\\"))); p++; } int IN = Convert.ToInt32(Console.ReadLine()); if (IN > p) { Console.WriteLine("编号错误"); string[] ccw = { in_str }; Main(ccw); } else if (IN >= c) { //文件 string[] cn = { directory.GetFiles()[IN - c].FullName }; Main(cn); } else { //文件夹 string[] cn = { directory.GetDirectories()[IN - 1].FullName }; Main(cn); } break; case "E": Environment.Exit(0); break; default: Console.WriteLine("输入有误"); string[] cc = { in_str }; Main(cc); break; } } else { Console.WriteLine(); Console.WriteLine("{0}是一个文件,请选择操作:", in_str); Console.WriteLine(" 【1】直接打开"); Console.WriteLine(" 【2】打开文件资源管理器,选中指定文件"); Console.WriteLine(" 【E】退出"); switch (Console.ReadLine()) { case "1": //打开文件资源管理器,浏览指定文件 Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\explorer.exe", in_str); break; case "2": //打开文件资源管理器,选中指定文件 Process.Start(System.Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\explorer.exe", "/select, " + in_str); break; case "E": Environment.Exit(0); break; default: Console.WriteLine("输入有误"); string[] cc = { in_str }; Main(cc); break; } } Console.WriteLine(); Console.WriteLine("所有操作已结束,请选择(若还未执行操作,请稍作等候)", in_str); Console.WriteLine(" 【1】退出"); Console.WriteLine(" 【2】继续"); switch (Console.ReadLine()) { case "1": Environment.Exit(0); break; case "2": Console.Clear(); Main(new string[0]); break; default: Environment.Exit(0); break; } } } }