tags:

views:

344

answers:

4

I created an installer using WiX. One thing the installer does is sets an environment variable PLUGIN_DIRECTORY (it's at the system level).

Within some C# code that I have written, I need to access that variable so I can watch a certain directory. I do this via the following code:

FileSystemWatcher water = new FileSystemWatcher();
watcher.Path = Environment.GetEnvironmentVariable("PLUGIN_DIRECTORY") + "\\";

Unfortunately (and when I debug), all that watcher.Path is set to is "\".

Do I need to reboot after the install? I wouldn't see why as the variable is already set. Any other suggestions? I'm not getting any errors - it's just not watching the right path.

Thanks

+2  A: 

If you had visual studio open when you created the environment variable then I don't know if it will pick it up until you close and restart VS. When a process is started it inherits the environment variables from it's parent process. I'm not exactly sure how VS launches an executable after you build it but it probably is a sub process and as a result isn't picking up your new environment variable.

Db
That was my problem - when I first performed the install, I wasn't correctly responding to events for the FileWatcher (AKA, my app didn't work). I fixed the app in Visual Studio, but when I tested in Visual Studio, it didn't pick up the new environment variable. Basically, it was two separate issues. Thank you for pointing me in the right direction!
JasCav
A: 

Just to be sure, does PLUGIN_DIRECTORY have anything set after the installer has run?

horsedrowner
Yes, it does. It is set to a path on the local drive (C:\Program Files\MyApp\Plugins)
JasCav
A: 

I'm sorry if I'm terribly wrong I can't confirm this as I do not have a compiler installed. But you can try to use: watcher.Path = Environment.GetEnvironmentVariable("%PLUGIN_DIRECTORY%") + "\";

That is, %PLUGIN_DIRECTORY% instead of just PLUGIN_DIRECTORY.

Hope it was of any help

Jonas B
A: 

the system environment is inheritance from parent, after update, other process can't recognized.

we could refresh process environment by load environment from "machine" and save to "process"

string SysEnvir = System.Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine);

System.Environment.SetEnvironmentVariable("Path", SysEnvir, EnvironmentVariableTarget.Process);