我写一个程序来杀死并重新启动资源管理器,但我不想硬码的位置,因为有些人安装Windows在不同的地方(例如,我发现有人谁曾它安装在d:\驱动器,其中C:\驱动确实存在,但在其上安装了什么)

我试图Environment.SpecialFolder下寻找。但我没有看到下一个“窗口”选项

什么是做到这一点的最好方法是什么?

有帮助吗?

解决方案

http://msdn.microsoft.com/en-us/library/ 77zkk0b6.aspx

尝试这些:

Environment.GetEnvironmentVariable("SystemRoot")

Environment.GetEnvironmentVariable("windir")

其他提示

Environment.GetFolderPath( Environment.SpecialFolder.Windows )将路径返回到Windows文件夹。推荐这种方式在环境变量,因为使用不正是我们想要的(.NET 4.0及以上)的API。

我会强烈推荐使用:

Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System))

它不需要管理员权限,并支持.NET框架的所有版本。

要简单地杀死,然后重新启动Windows资源管理器,你将不需要的路径到系统文件夹,因为这已经包含在PATH环境变量(除非用户用它搞砸)。

这短程序将杀死所有的explorer.exe实例,然后重新启动的explorer.exe:

static void Main(string[] args)
{
    foreach (Process process in Process.GetProcessesByName("explorer"))
    {
        if (!process.HasExited)
        {
            process.Kill();
        }
    }
    Process.Start("explorer.exe");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top