How to Get the Current User Directory

How can I get the current user directory?

May be this will be a good solution: taking in account whether this is Vista/Win7 or XP and without using environment variables:

string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
if ( Environment.OSVersion.Version.Major >= 6 ) {
path = Directory.GetParent(path).ToString();
}

Though using the environment variable is much more clear.

How to get the current user's home directory in Windows

Use the function SHGetFolderPath. This function is preferred over querying environment variables since the latter can be modified to point to a wrong location. The documentation contains an example, which I repeat here (slightly adjusted):

#include <Shlobj.h>  // need to include definitions of constants

// .....

WCHAR path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path))) {
...
}

Get path to the user home directory on Windows in PowerShell?

Use the environment variable $env:USERPROFILE.

For example:

cd "$env:USERPROFILE\Downloads"

will take you to the user's Downloads folder (given that it's named "Downloads").]

How to Change to current user's directory in a batch file

Use %HOMEPATH%.

So:

CD %HOMEPATH%

c# service: how to get user profile folder path

A service doesn't log on like a user, unless the service is configured to use a specific user's profile. So it's not going to point to "user" folders.

How to get %HOMEPATH% of file system from java

String usersHomeDir = System.getProperty("user.home");

Get windows system folders (user home directory, My documents, etc) path in R

I think you want:

path.expand('~')


Related Topics



Leave a reply



Submit