Jimy Weiss
Jimy Weiss

Reputation: 678

IIS cannot get my environment variable

I would like to use environment variable in my web application.

I go to the advanced system settings and set my environment variable (for example MY_HOST_NAME) by system-level variables. Then I restart the server and check with the commandline "set" if the variable is there and I can see it. But if I try to get the variable within my application which is hosted by IIS, there is the variable null. The code which I'm using:

var host = Environment.GetEnvironmentVariable("MY_HOST_NAME");

Knows anyone where the problem could be or how can I find the bug?

Upvotes: 12

Views: 10515

Answers (2)

Dan Dohotaru
Dan Dohotaru

Reputation: 3079

If by restarting server you mean Restart from IIS Manager, then this will not be enough. In case you don't want to restart the machine (who does?), you'll have to restart iis from an elevated prompt to pickup the newly changed variables

iisreset /restart

or in case one prefers some more typing

net stop w3svc && net start w3svc

Upvotes: 31

rory.ap
rory.ap

Reputation: 35260

Is it a user environmental variable, or a system-level variable? The reason I ask is your IIS code is probably running as a different user. If your variable is a user variable, it wouldn't be present when the code runs as the other user.

Go to the control panel, system properties:

enter image description here

Upvotes: 4

Related Questions