KrakenZ
KrakenZ

Reputation: 25

How to get root directory of windows using C#?

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?

Upvotes: 0

Views: 667

Answers (3)

KrakenZ
KrakenZ

Reputation: 25

 DirectoryInfo di2 = new DirectoryInfo(Environment.GetEnvironmentVariable("SystemRoot"));
            string path = Convert.ToString(di2);
            
            DirectoryInfo di = new DirectoryInfo( path + @"\temp");

I figured it out. Here is the corrected code.

Upvotes: 0

AlexM
AlexM

Reputation: 68

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

Upvotes: 2

Eterm
Eterm

Reputation: 1808

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

Upvotes: 2

Related Questions