windows 检索系统驱动器的最安全方法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/910004/
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 12:32:57  来源:igfitidea点击:

What is the most secure way to retrieve the system Drive

c#.netwindowsenvironment-variablessystem-administration

提问by user88637

I know that the following should work:

我知道以下应该有效:

Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine) 

My problem with this call is that if for some reason someone decided to remove the "windir" Env Var , this won't work.

我对这个调用的问题是,如果由于某种原因有人决定删除“windir” Env Var ,这将不起作用。

Is there an even more secure way to get the System drive?

有没有更安全的方法来获取系统驱动器?

采纳答案by Oliver

One thing i actually maybe misunderstand is that you want the System Drive, but by using "windir" you'll get the windows folder. So if you need a secure wayto get the windows folder, you should use the good old API function GetWindowsDirectory.

我实际上可能误解的一件事是您想要系统驱动器,但是通过使用“windir”,您将获得 windows 文件夹。因此,如果您需要一种安全的方式来获取 windows 文件夹,您应该使用很好的旧 API 函数 GetWindowsDirectory。

Here is the function prepared for C# usage. ;-)

这是为 C# 使用准备的函数。;-)

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern uint GetWindowsDirectory(StringBuilder lpBuffer, uint uSize);

    private string WindowsDirectory()
    {
        uint size = 0;
        size = GetWindowsDirectory(null, size);

        StringBuilder sb = new StringBuilder((int)size);
        GetWindowsDirectory(sb, size);

        return sb.ToString();
    }

So if you really need the drive on which windows is running, you could afterwards call

因此,如果您确实需要运行 Windows 的驱动器,您可以事后调用

System.IO.Path.GetPathRoot(WindowsDirectory());

回答by Serge Wautier

string windir = Environment.SystemDirectory; // C:\windows\system32
string windrive = Path.GetPathRoot(Environment.SystemDirectory); // C:\

Note: This property internally uses the GetSystemDirectory() Win32 API. It doesn't rely on environment variables.

注意:此属性在内部使用 GetSystemDirectory() Win32 API。它不依赖于环境变量。

回答by Fredrik M?rk

This one returns the path to the system directory (system32).

这将返回系统目录 (system32) 的路径。

Environment.GetFolderPath(Environment.SpecialFolder.System)

You may be able to use that, then you don't need to rely on environment variables.

您也许可以使用它,那么您就不需要依赖环境变量了。

回答by Richard Szalay

You can use the GetWindowsDirectoryAPI to retrieve the windows directory.

您可以使用GetWindowsDirectoryAPI 来检索 Windows 目录。

回答by Richard Szalay

Never read environment variables (any script or user can change them !)
The officialmethod (MS internal, used by Explorer) is a Win32 api FAQ for decades (see Google groups, Win32, System api)

从来不看环境变量(任何脚本或用户可以改变它们!)
官方方法(MS内部,通过浏览器使用)是几十年来的Win32 API常见问题解答(见谷歌组,Win32中,系统API)

回答by Eoin Campbell

Theres an environment variable called SystemDrive

有一个环境变量叫做 SystemDrive

C:\>SET SystemDrive
SystemDrive=C: