在 C# 中以编程方式查找 windows 文件夹
我正在编写一个程序来终止并重新启动资源管理器,但我不想对位置进行硬编码,因为有些人将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://msdn.microsoft.com/en-us/library/77zkk0b6.aspx尝试
这些:
http://msdn.microsoft.com/en-us/library/77zkk0b6.aspx
Try these:
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).我强烈建议使用:
它不需要管理员权限,并且支持所有版本的 .NET 框架。
I would highly recommend the use of:
It does NOT require administrator rights and supports all versions of the .NET framework.
要简单地终止并重新启动 Windows 资源管理器,您不需要系统文件夹的路径,因为它已经包含在 PATH 环境变量中(除非用户弄乱了它)。
该短程序将杀死所有 explorer.exe 实例,然后重新启动 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: