• 0

[C#] Get Windows Drive


Question

Is there a built in function to get the location (drive letter) of the windows drive?

Is the best way to do this just to check for the boot.ini? Was just wondering if there is a more definitive way.

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

You can use the GetWindowsDirectory(...) API call and then get the drive letter from that:

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

		public static string WindowsDirectory
		{
			get
			{
				StringBuilder builder = new StringBuilder(255);
				int length = (int)GetWindowsDirectory(builder, 255);
				return builder.ToString(0, length).ToLower();
			}
		}

		public static string WindowsDrive
		{
			get 
			{
				return WindowsDirectory.Substring(0,1);
			}
		}
	}

Link to comment
Share on other sites

  • 0

An easier way that uses the framework

 Environment.GetFolderPath(Environment.SpecialFolder.System).Substring(0,1)

This returns the path to drive:\\Windows\System32 folder so you just pull off the drive letter from the left.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.