假设路径 C:\WINDOWS\system32 始终存在是否安全?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/239209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:31:42  来源:igfitidea点击:

Is it safe to assume that the path C:\WINDOWS\system32 always exists?

windowsoperating-system

提问by Ohad Horesh

On OS from win 2000 or later (any language) can I assume that this path will always exists? For example I know that on win xp in some languages the "Program Files" directory have a different name. So is it true for the System32 folder?

在 win 2000 或更高版本(任何语言)的操作系统上,我可以假设此路径将始终存在吗?例如,我知道在某些语言的 win xp 上,“Program Files”目录具有不同的名称。那么 System32 文件夹是真的吗?

Thanks. Ohad.

谢谢。哦哈。

回答by DavidK

You definitely cannot assume that: Windows could be installed on a different drive letter, or in a different directory. On a previous work PC Windows was installed in D:\WINNT, for example.

您绝对不能假设:Windows 可以安装在不同的驱动器号或不同的目录中。例如,在以前的工作 PC 上,Windows 安装在 D:\WINNT 中。

The short answer is to use the API call GetSystemDirectory(), which will return the path you are after.

简短的回答是使用 API 调用 GetSystemDirectory(),它将返回您所追求的路径。

The longer answer is to ask: do you really need to know this? If you're using it to copy files into the Windows directory, I'd suggest you ask if you really want to do this. Copying into the Windows directory is not encouraged, as you can mess up other applications very easily. If you're using the path to find DLLs, why not just rely on the OS to find the appropriate one without giving a path? If you're digging into bits of the OS files, consider: is that going to work in future? In general it's better to not explicitly poke around in the Windows directory if you want your program to work on future Windows versions.

更长的答案是问:你真的需要知道这个吗?如果您使用它来将文件复制到 Windows 目录中,我建议您询问您是否真的要这样做。不鼓励复制到 Windows 目录中,因为您很容易弄乱其他应用程序。如果您使用路径来查找 DLL,为什么不直接依靠操作系统来查找合适的 DLL 而不提供路径呢?如果您正在深入研究操作系统文件的一部分,请考虑:这将来会起作用吗?一般来说,如果您希望您的程序在未来的 Windows 版本上运行,最好不要在 Windows 目录中明确地进行检查。

回答by vmarquez

No, you can't assume that.

不,你不能假设。

Windows can be installed to a different path. One solution is to look for it by calling GetSystemDirectory (implemented as part of the Windows API).

Windows 可以安装到不同的路径。一种解决方案是通过调用 GetSystemDirectory(作为 Windows API 的一部分实现)来查找它。

回答by Jasper Bekkers

Windows can be installed on a different harddrive and or in a different folder. Use the %windir% or %systemroot% environment variables to get you to the windows folder and append system32. Or use the %path% variable, it's usually the first entrance and the preferred method of searching for files such as dlls AFAIK. As per comments: don't rely too much on the system32 dir being the first item. I do think it's safe to assume it's in %path% somewhere though.

Windows 可以安装在不同的硬盘驱动器和/或不同的文件夹中。使用 %windir% 或 %systemroot% 环境变量将您带到 windows 文件夹并附加 system32。或者使用 %path% 变量,它通常是搜索 dll AFAIK 等文件的第一个入口和首选方法。根据评论:不要过分依赖 system32 目录作为第一项。我确实认为可以安全地假设它在 %path% 的某个地方。

回答by jussij

I would use the GetWindowsDirectoryWin32 API to get the current Windows directory, append System32to it an then check if it exists.

我会使用GetWindowsDirectoryWin32 API 来获取当前的 Windows 目录,将System32附加到它然后检查它是否存在。

回答by Jarod Elliott

It might be safer to use the "windir" environment variable and then append the "System32" to the end of that path. Sometimes windows could be under a different folder or different drive so "windir" will tell you where it is.

使用“windir”环境变量然后将“System32”附加到该路径的末尾可能更安全。有时 Windows 可能位于不同的文件夹或不同的驱动器下,因此“windir”会告诉您它在哪里。

As far as i know, the system32 folder should always exist under the windows folder.

据我所知,system32 文件夹应该始终存在于 windows 文件夹下。

回答by Jarod Elliott

Just an FYI, but in a Terminal Server environment (ie, Citrix), GetWindowsDirectory() may return a unique path for a remote user.

仅供参考,但在终端服务器环境(即 Citrix)中,GetWindowsDirectory() 可能会为远程用户返回唯一路径。

link text

链接文字

As more and more companies use virtualized desktops, developers need to keep this in mind.

随着越来越多的公司使用虚拟化桌面,开发人员需要牢记这一点。