예제 #1
0
        /// <summary>
        /// Sets an environment variable for the user.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void SetUsersEnvironmentVariable(string name, string value)
        {
            if (!this.Created)
            {
                throw new InvalidOperationException("ProcessPrison has to be created first.");
            }

            if (string.IsNullOrEmpty(name) || name.Contains('='))
            {
                throw new ArgumentException("Invalid name", "name");
            }

            if (value == null)
            {
                throw new ArgumentException("Value is null", "value");
            }

            this.myenvvars[name] = value;

            using (var impersonator = new UserImpersonator(this.WindowsUsername, this.WindowsDomain, this.WindowsPassword, true))
            {
                using (var registryHandle = impersonator.GetRegistryHandle())
                {
                    using (var registry = RegistryKey.FromHandle(registryHandle))
                    {
                        using (var envRegKey = registry.OpenSubKey("Environment", true))
                        {
                            envRegKey.SetValue(name, value, RegistryValueKind.String);
                        }
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Sets an environment variable for the user.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void SetUsersEnvironmentVariables(Dictionary<string, string> envVariables)
        {
            if (!this.Created)
            {
                throw new InvalidOperationException("ProcessPrison has to be created first.");
            }

            if (envVariables.Keys.Any(x => x.Contains('=')))
            {
                throw new ArgumentException("A name of an environment variable contains the invalid '=' characther", "envVariables");
            }

            if (envVariables.Keys.Any(x => string.IsNullOrEmpty(x)))
            {
                throw new ArgumentException("A name of an environment variable is null or empty", "envVariables");
            }

            //if (envVariables.Values.Any(x => x == null))
            //{
            //    throw new ArgumentException("A value of an environment variable is null", "envVariables");
            //}

            foreach (string key in envVariables.Keys)
            {
                this.myenvvars[key] = envVariables[key];
            }

            using (var impersonator = new UserImpersonator(this.WindowsUsername, this.WindowsDomain, this.WindowsPassword, true))
            {
                using (var registryHandle = impersonator.GetRegistryHandle())
                {
                    using (var registry = RegistryKey.FromHandle(registryHandle))
                    {
                        using (var envRegKey = registry.OpenSubKey("Environment", true))
                        {
                            foreach (var env in envVariables)
                            {
                                var value = env.Value == null ? string.Empty : env.Value;

                                envRegKey.SetValue(env.Key, value, RegistryValueKind.String);
                            }
                        }
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Gets the environment variables for the user.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public IDictionary<string, string> GetUsersEnvironmentVariables()
        {
            if (!this.Created)
            {
                throw new InvalidOperationException("ProcessPrison has to be created first.");
            }

            var ret = new Dictionary<string, string>();

            using (var impersonator = new UserImpersonator(this.WindowsUsername, this.WindowsDomain, this.WindowsPassword, true))
            {
                using (var registryHandle = impersonator.GetRegistryHandle())
                {
                    using (var registry = RegistryKey.FromHandle(registryHandle))
                    {
                        using (var envRegKey = registry.OpenSubKey("Environment", true))
                        {
                            foreach (var key in envRegKey.GetValueNames())
                            {
                                ret[key] = (string)envRegKey.GetValue(key);
                            }
                        }
                    }
                }
            }

            return ret;
        }