Exemplo n.º 1
0
        /// <summary>Creates, modifies, or deletes an environment variable stored in the current process or in the Windows operating system registry key reserved for the current user or local machine.</summary>
        /// <param name="variable">The name of an environment variable.</param>
        /// <param name="value">A value to assign to <paramref name="variable" />. </param>
        /// <param name="target">One of the <see cref="T:System.EnvironmentVariableTarget" /> values.</param>
        /// <exception cref="T:System.ArgumentNullException">
        ///   <paramref name="variable" /> is null. </exception>
        /// <exception cref="T:System.ArgumentException">
        ///   <paramref name="variable" /> contains a zero-length string, an initial hexadecimal zero character (0x00), or an equal sign ("="). -or-The length of <paramref name="variable" /> is greater than or equal to 32,767 characters.-or-<paramref name="target" /> is not a member of the <see cref="T:System.EnvironmentVariableTarget" /> enumeration. -or-<paramref name="target" /> is <see cref="F:System.EnvironmentVariableTarget.Machine" /> or <see cref="F:System.EnvironmentVariableTarget.User" /> and the length of <paramref name="variable" /> is greater than or equal to 255.-or-<paramref name="target" /> is <see cref="F:System.EnvironmentVariableTarget.Process" /> and the length of <paramref name="value" /> is greater than or equal to 32,767 characters. -or-An error occurred during the execution of this operation.</exception>
        /// <exception cref="T:System.NotSupportedException">
        ///   <paramref name="target" /> is <see cref="F:System.EnvironmentVariableTarget.User" /> or <see cref="F:System.EnvironmentVariableTarget.Machine" /> and the current operating system is Windows 95, Windows 98, or Windows Me.</exception>
        /// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission to perform this operation. </exception>
        /// <filterpriority>1</filterpriority>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        ///   <IPermission class="System.Security.Permissions.RegistryPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode" />
        /// </PermissionSet>
        public static void SetEnvironmentVariable(string variable, string value, EnvironmentVariableTarget target)
        {
            if (variable == null)
            {
                throw new ArgumentNullException("variable");
            }
            if (variable == string.Empty)
            {
                throw new ArgumentException("String cannot be of zero length.", "variable");
            }
            if (variable.IndexOf('=') != -1)
            {
                throw new ArgumentException("Environment variable name cannot contain an equal character.", "variable");
            }
            if (variable[0] == '\0')
            {
                throw new ArgumentException("The first char in the string is the null character.", "variable");
            }
            switch (target)
            {
            case EnvironmentVariableTarget.Process:
                Environment.InternalSetEnvironmentVariable(variable, value);
                break;

            case EnvironmentVariableTarget.User:
                if (!Environment.IsRunningOnWindows)
                {
                    return;
                }
                using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Environment", true))
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        registryKey.DeleteValue(variable, false);
                    }
                    else
                    {
                        registryKey.SetValue(variable, value);
                    }
                    Environment.internalBroadcastSettingChange();
                }
                break;

            case EnvironmentVariableTarget.Machine:
                if (!Environment.IsRunningOnWindows)
                {
                    return;
                }
                using (RegistryKey registryKey2 = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", true))
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        registryKey2.DeleteValue(variable, false);
                    }
                    else
                    {
                        registryKey2.SetValue(variable, value);
                    }
                    Environment.internalBroadcastSettingChange();
                }
                break;

            default:
                throw new ArgumentException("target");
            }
        }