Home > Software engineering >  How to get root directory of windows using C#?
How to get root directory of windows using C#?

Time:10-10

DirectoryInfo di = new DirectoryInfo(@"c:\windows\temp");

Here I want to replace c:\ with the current drive on which the user is operating windows. Is it possible to do that?

CodePudding user response:

In .net you ought to use Path.GetTempPath to get a temporary path:

https://learn.microsoft.com/en-us/dotnet/api/system.io.path.gettemppath?view=net-7.0&tabs=windows

CodePudding user response:

Both Environment.GetEnvironmentVariable("SystemRoot") and Environment.GetEnvironmentVariable("windir") should give you the path in form of "driveLetter:\\Windows"

So you can do:

DirectoryInfo di = new DirectoryInfo(Environment.GetEnvironmentVariable("SystemRoot"));

For the difference between "SystemRoot" and "windir" see: https://superuser.com/questions/638321/what-is-difference-between-windir-and-systemroot

  • Related