문제

다음이 작동해야한다는 것을 알고 있습니다.

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

이 전화의 내 문제는 어떤 이유로 든 누군가가 "windir"env var를 제거하기로 결정했다면 이것이 작동하지 않는다는 것입니다.

시스템을 추진할 수있는 더 안전한 방법이 있습니까?

도움이 되었습니까?

해결책

내가 실제로 오해하는 한 가지는 시스템 드라이브를 원하지만 "Windir"를 사용하면 Windows 폴더를 얻을 수 있다는 것입니다. 그래서 당신이 필요하다면 안전한 방법 Windows 폴더를 얻으려면 Good Old API 기능 GetWindowSdirectory를 사용해야합니다.

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();
    }

따라서 Windows가 실행중인 드라이브가 실제로 필요하면 나중에 전화 할 수 있습니다.

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

다른 팁

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

참고 :이 속성은 내부적으로 getSystemDirectory () Win32 API를 사용합니다. 환경 변수에 의존하지 않습니다.

이것은 시스템 디렉토리 (System32)로의 경로를 반환합니다.

Environment.GetFolderPath(Environment.SpecialFolder.System)

이를 사용할 수 있고 환경 변수에 의존 할 필요가 없습니다.

당신은 사용할 수 있습니다 getwindowsdirectory API는 Windows 디렉토리를 검색합니다.

환경 변수를 읽지 마십시오 (모든 스크립트 또는 사용자는 변경할 수 있습니다!)
그만큼 공식적인 방법 (MS 내부, Explorer에서 사용)은 수십 년 동안 Win32 API FAQ입니다 (Google Groups, Win32, System API 참조)

호출되는 환경 변수가 있습니다 SystemDrive

C:\>SET SystemDrive
SystemDrive=C:
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top