Cra*_*893 38 c# windows directory special-folders
我正在编写一个程序来杀死并重新启动资源管理器但是我不想硬编码位置,因为有些人在不同的地方安装了Windows(例如我发现有人将它安装在d:\驱动器所在的C:\驱动器确实存在,但没有安装任何东西)
我试过在Environment.SpecialFolder下查找.但我没有看到"windows"选项
做这个的最好方式是什么?
Oma*_*mar 66
http://msdn.microsoft.com/en-us/library/77zkk0b6.aspx
试试这些:
Environment.GetEnvironmentVariable("SystemRoot")
Environment.GetEnvironmentVariable("windir")
Run Code Online (Sandbox Code Playgroud)
Uri*_*Uri 44
Environment.GetFolderPath( Environment.SpecialFolder.Windows )
将返回Windows文件夹的路径.推荐这种方法优于环境变量,因为使用的API完全符合我们的要求(.NET 4.0及更高版本).
Ada*_*say 15
我强烈建议使用:
Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.System))
Run Code Online (Sandbox Code Playgroud)
它不需要管理员权限,并且支持所有版本的.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");
}
Run Code Online (Sandbox Code Playgroud)