How to expand environment variables remotely with .NET?

后端 未结 4 1972
滥情空心
滥情空心 2020-12-11 16:10

I need a way to expand environment variable on a remote machine.

Suppose I have a path to a folder %appdata%\\MyApp\\Plugins or %ProgramFiles%\\My

4条回答
  •  Happy的楠姐
    2020-12-11 16:39

    Environment variables are the amalgamation of 'puter-wide and per-user settings. A running process may modify its environment and when it spawns another process, that process inherits the environment of the process that created it.

    Unless you have access to a process running on the remote machine (or can start one), there's no such thing as an 'environment': the context for it simply doesn't exist. The environment of a particular process is a function of all of the following:

    • the environment inherited from the parent process' environment (which may be running under a different user account than the child process.)
    • computer-wide environment settings.
    • any environment settings specified by the user.
    • any changes made by the process itself.

    That being said, Windows keeps its environment variable settings in the registry:

    • User variables.
      HKEY_CURRENT_USER\Environment
    • System variables.
      HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

    If you have appropriate access to the remote machine's registry, you should be able to fish out what you need.

    Note that environment variables may be defined in terms of other environment variables: I believe you'll likely to take care of the proper expansion yourself.

提交回复
热议问题