.NET Framework Class Library
Environment..::.GetFolderPath Method

Updated: October 2009

Gets the path to the system special folder identified by the specified enumeration.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Shared Function GetFolderPath ( _
    folder As Environment..::.SpecialFolder _
) As String
Visual Basic (Usage)
Dim folder As Environment..::.SpecialFolder
Dim returnValue As String

returnValue = Environment.GetFolderPath(folder)
C#
public static string GetFolderPath(
    Environment..::.SpecialFolder folder
)
Visual C++
public:
static String^ GetFolderPath(
    Environment..::.SpecialFolder folder
)
JScript
public static function GetFolderPath(
    folder : Environment..::.SpecialFolder
) : String

Parameters

folder
Type: System..::.Environment..::.SpecialFolder
An enumerated constant that identifies a system special folder.

Return Value

Type: System..::.String
The path to the specified system special folder, if that folder physically exists on your computer; otherwise, the empty string ("").
A folder will not physically exist if the operating system did not create it, the existing folder was deleted, or the folder is a virtual directory, such as My Computer, which does not correspond to a physical path.
Exceptions

ExceptionCondition
ArgumentException

folder is not a member of System..::.Environment..::.SpecialFolder.

PlatformNotSupportedException

The current platform is not supported.

Remarks

This method retrieves the path to a system special folder, such as Program Files, Programs, System, or Startup, which can be used to access common information. Special folders are set by default by the system, or explicitly by the user, when installing a version of Windows.

The folder parameter designates the special folder to retrieve and must be one of the values in the Environment..::.SpecialFolder enumeration; any other value throws an exception.

For more information about special folders, see the CSIDL values topic.

Examples

The following example demonstrates how to use the GetFolderPath method to return and display the path associated with the folder parameter.

Visual Basic
' Sample for the Environment.GetFolderPath method
Imports System

Class Sample
   Public Shared Sub Main()
      Console.WriteLine()
      Console.WriteLine("GetFolderPath: {0}", Environment.GetFolderPath(Environment.SpecialFolder.System))
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'GetFolderPath: C:\WINNT\System32
'
C#
// Sample for the Environment.GetFolderPath method
using System;

class Sample 
{
    public static void Main() 
    {
    Console.WriteLine();
    Console.WriteLine("GetFolderPath: {0}", 
                 Environment.GetFolderPath(Environment.SpecialFolder.System));
    }
}
/*
This example produces the following results:

GetFolderPath: C:\WINNT\System32
*/
Visual C++
// Sample for the Environment::GetFolderPath method
using namespace System;
int main()
{
   Console::WriteLine();
   Console::WriteLine( "GetFolderPath: {0}", Environment::GetFolderPath( Environment::SpecialFolder::System ) );
}

/*
This example produces the following results:

GetFolderPath: C:\WINNT\System32
*/
.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 2.0, 1.0
See Also

Reference

Change History

Date

History

Reason

October 2009

Added PlatformNotSupportedException to the exceptions.

Information enhancement.

Tags :


Community Content

eschneider001
May behave differently depending on what root process is calling the function

May behave differently depending on what root process is calling the function:

Example:
Dim p As String
p = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles)

Returns the following in design mode (Win Forms Designer):
"C:\Program Files (x86)\Common Files"

Returns the following in runtime mode (exe application):
"C:\Program Files\Common Files"

I assume this is because my application is 64bit, while the designer (VS2008) is 32bit...


Felix Collins
The path to Common Documents

GetFolderPath cannot be used to obtain the path to Common Documents folder.
However, you can use the following COM code for the purpose:

(New Shell32.Shell).NameSpace(46).Items.Item.Path

(refer to Windows Shell Automation Controls library for this method to work)



In .net (C# example) you could also use...

Environment.ExpandEnvironmentVariables("%PUBLIC%);

Tags :

Page view tracker