开发者

Mono C# environment set variable

开发者 https://www.devze.com 2023-03-11 16:57 出处:网络
How can I set an environment variable from my application? I don\'t need a persistent variable (OK to discard after a reboot).

How can I set an environment variable from my application?

I don't need a persistent variable (OK to discard after a reboot). I just need the variable MONO_RX "surviving" my application's end.


Environment.SetEnvironmentVariable( "MONO", txt, EnvironmentVariableTarget.User );
Environment.SetEnvironmentVariable( "MONO", txt, EnvironmentVariableTarget.Process );
Process.Start( "/bin/bash", "-c MONO=" + txt + "; export MONO; echo $MONO" );
Process.Start( "/bin/bash", "-c MONO=" + tx开发者_JAVA技巧t + "; export MONO; echo $MONO \r\n" );

sudo mono test.exe

gives no errors, but after my console application "test" quits, I don't find $MONO:

echo $MONO

set | grep MONO

System:

  • linux 2.6.38-8-generic PAE
  • Mono JIT compiler version 2.6.7 (Debian 2.6.7-5ubuntu3)


Env variables are shared with the children of the process.

If you set some env var in your process, only the processes started from it will see them, after your process terminates, they are gone forever.

You probably just want to set them in the shell instead.


Have you tried running this with EnvironmentVariableTarget.Machine. EnvironmentVariableTarget .Process causes the entry to be removed when the process terminates. .User is specific to the user running the process.

You're using sudo to run the application, thus "sudo echo $Mono" will give you the expected result because that is the user you created the environment variable under, not your user (or you wouldn't be using sudo).

Remove EnvironmentVariableTarget.Process either way. You're looking for access external to your particular process. After that, do one of the following:

  • Make a Machine level environment variable.
  • Run your application as the user expected
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号