DanielVest
DanielVest

Reputation: 823

Why is this environment variable always null?

I want to automatically get the directory: user\mydocuments So I did:

t = Environment.GetEnvironmentVariable(Environment.GetFolderPath(Environment.SpecialFolder.Personal));

But t is null all the time.

Upvotes: 0

Views: 1211

Answers (1)

User 12345678
User 12345678

Reputation: 7804

The source of the problem is that you are calling Environment.GetEnvironmentVariable when you really don't need to.

Your code successfully obtains the directory path but then you proceeded pass said directory path to GetEnvironmentVariable() which in turn, proceeds to look at the system's environment variables for an environment variable called "user\my_documents". Because no such environment variable exists the function will return null.

Simply do not pass the directory path to GetEnvironmentVariable() and your code should function as expected:

var foo = 
     Environment.GetFolderPath(Environment.SpecialFolder.Personal);

Upvotes: 5

Related Questions