在 C# 中以编程方式查找 windows 文件夹

发布于 2024-08-06 21:33:23 字数 204 浏览 2 评论 0原文

我正在编写一个程序来终止并重新启动资源管理器,但我不想对位置进行硬编码,因为有些人将 Windows 安装在不同的位置(例如,我发现有人将其安装在 d:\ 驱动器中,其中 C:\驱动器确实存在,但上面没有安装任何东西)

我尝试在Environment.SpecialFolder下查找。但我在该选项下没有看到“Windows”选项,

执行此操作的最佳方法是什么?

I am writing a program to kill and restart explorer but I don't want to hard code the location because some people install windows in different places (for example I found someone who had it installed in the d:\ drive where the C:\ drive did exist but had nothing installed on it)

I tried looking under Environment.SpecialFolder. but I don't see a "windows" option under that

What is the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离鸿 2024-08-13 21:33:23

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

这些:

Environment.GetEnvironmentVariable("SystemRoot")

Environment.GetEnvironmentVariable("windir")

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

Try these:

Environment.GetEnvironmentVariable("SystemRoot")

Environment.GetEnvironmentVariable("windir")
夏夜暖风 2024-08-13 21:33:23

Environment.GetFolderPath(Environment.SpecialFolder.Windows) 将返回 Windows 文件夹的路径。推荐使用此方法而不是环境变量,因为使用的 API 可以完全满足我们的需求(.NET 4.0 及更高版本)。

Environment.GetFolderPath( Environment.SpecialFolder.Windows ) will return the path to the Windows folder. Recommend this approach over the environment variable, because using an API that does exactly what we want (.NET 4.0 and above).

彡翼 2024-08-13 21:33:23

我强烈建议使用:

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

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

I would highly recommend the use of:

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

It does NOT require administrator rights and supports all versions of the .NET framework.

永言不败 2024-08-13 21:33:23

要简单地终止并重新启动 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");
}

To simply kill and restart Windows Explorer you wouldn't need the path to the system folder as this is already included in the PATH environment variable (unless the user messed with it).

That short program will kill all explorer.exe instances and then restart explorer.exe:

static void Main(string[] args)
{
    foreach (Process process in Process.GetProcessesByName("explorer"))
    {
        if (!process.HasExited)
        {
            process.Kill();
        }
    }
    Process.Start("explorer.exe");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文