Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Gurus*
Posted

You can use Environment.SystemDirectory to retreive the path to the system directory, and use string manipulation or the methods of the Path class to get just the Windows directory from that.

 

As far as I know there's no framework way to get just the Windows directory, but I'd like to be proved wrong.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

Use DirectoryInfo

 

System (or System32) is always a direct sub-folder of Windows (or Win2k/NT or whatever) so use the following code:

VB:
Public Function GetWinDir() as String
    'Create a directory info structure of C:\Win2k\System32 or C:\Windows\system, etc.
    Dim di as New DirectoryInfo(Environment.SystemDirectory)
    'Return the full path of its parent
    Return di.Parent.FullName
End Function
C#:
public String GetWinDir()
{
    //Create a directory info structure of C:\Win2k\System32 or C:\Windows\system, etc.
    DirectoryInfo di = new DirectoryInfo(Environment.SystemDirectory)
    // Return the full path of its parent
    return di.Parent.FullName
}

No matter the Windows version this should return the windows dir

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...