当前位置: 首页>>代码示例>>C#>>正文


C# EnvironmentVariableTarget类代码示例

本文整理汇总了C#中EnvironmentVariableTarget的典型用法代码示例。如果您正苦于以下问题:C# EnvironmentVariableTarget类的具体用法?C# EnvironmentVariableTarget怎么用?C# EnvironmentVariableTarget使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


EnvironmentVariableTarget类属于命名空间,在下文中一共展示了EnvironmentVariableTarget类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetEnvironmentVariableCore

        private static string GetEnvironmentVariableCore(string variable, EnvironmentVariableTarget target)
        {
            if (target == EnvironmentVariableTarget.Process)
            {
                return GetEnvironmentVariableCore(variable);
            }
            else
            {
                RegistryKey baseKey;
                string keyName;

                if (target == EnvironmentVariableTarget.Machine)
                {
                    baseKey = Registry.LocalMachine;
                    keyName = @"System\CurrentControlSet\Control\Session Manager\Environment";
                }
                else
                {
                    Debug.Assert(target == EnvironmentVariableTarget.User);
                    baseKey = Registry.CurrentUser;
                    keyName = "Environment";
                }

                using (RegistryKey environmentKey = baseKey.OpenSubKey(keyName, writable: false))
                {
                    return environmentKey?.GetValue(variable) as string;
                }
            }
        }
开发者ID:geoffkizer,项目名称:corefx,代码行数:29,代码来源:Environment.Win32.cs

示例2: GetEnvironmentVariables

        /// <summary>
        /// Retrieves all environment variable names and their values
        /// from the Windows operating system registry key for the
        /// current user or local machine.
        /// </summary>
        /// <param name="target">
        /// One of the <see cref="System.EnvironmentVariableTarget" /> values.
        /// </param>
        /// <returns>
        /// A dictionary that contains all environment variables
        /// names and their values from the source specified by the
        /// target parameter; otherwise, an empty dictionary if no
        /// environment variables are found.
        /// </returns>
        public static IDictionary<string, string> GetEnvironmentVariables(EnvironmentVariableTarget target)
        {
            Dictionary<string, string> variables = new Dictionary<string, string>();

            string keyName;
            RegistryKey registryKey;

            switch (target)
            {
                case EnvironmentVariableTarget.Machine:
                    keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
                    registryKey = Registry.LocalMachine.OpenSubKey(keyName);
                    break;
                case EnvironmentVariableTarget.User:
                    keyName = @"Environment";
                    registryKey = Registry.CurrentUser.OpenSubKey(keyName);
                    break;
                default:
                    throw new ArgumentException("Invalid environment variable target", "target");
            }

            foreach (string name in registryKey.GetValueNames())
            {
                variables.Add(name, (string)registryKey.GetValue(name, null, RegistryValueOptions.DoNotExpandEnvironmentNames));
            }

            return variables;
        }
开发者ID:visemet,项目名称:path-manager,代码行数:42,代码来源:Proxy.cs

示例3: SetEnvironmentVariable

        /// <summary>
        /// Creates, modifies, or deletes an environment variable stored
        /// 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 variable.</param>
        /// <param name="target">
        /// One of the <see cref="System.EnvironmentVariableTarget" /> values.
        /// </param>
        public static void SetEnvironmentVariable(string variable, string value, EnvironmentVariableTarget target)
        {
            string keyName;
            RegistryKey registryKey;

            switch (target)
            {
                case EnvironmentVariableTarget.Machine:
                    keyName = @"SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
                    registryKey = Registry.LocalMachine.OpenSubKey(keyName, true);
                    break;
                case EnvironmentVariableTarget.User:
                    keyName = @"Environment";
                    registryKey = Registry.CurrentUser.OpenSubKey(keyName, true);
                    break;
                default:
                    throw new ArgumentException("Invalid environment variable target", "target");
            }

            if (value == null)
            {
                registryKey.DeleteValue(variable, false);
            }
            else
            {
                registryKey.SetValue(variable, value, RegistryValueKind.ExpandString);
            }

            Notification.EnvironmentChanged();
        }
开发者ID:visemet,项目名称:path-manager,代码行数:42,代码来源:Proxy.cs

示例4: GetExisting

        private string GetExisting(EnvironmentVariableTarget target, string variableName)
        {
            var existingVariable = _variables[target][variableName];
            if (!IsValid(existingVariable)) UpdateVariable(existingVariable);

            return existingVariable.Content;
        }
开发者ID:BADF00D,项目名称:Tanpohp,代码行数:7,代码来源:EnvironmentVariableProvider.cs

示例5: AllVariables

 public static void AllVariables(EnvironmentVariableTarget target)
 {
     foreach (DictionaryEntry var in Environment.GetEnvironmentVariables(target))
     {
         string key = (string)var.Key;
         string value = (string)var.Value;
         ConsoleHelper.WriteLine(key + ": " + value);
     }
 }
开发者ID:cwillison94,项目名称:sar-tool,代码行数:9,代码来源:StringHelper.cs

示例6: Get

 public string Get(string variableName, EnvironmentVariableTarget target)
 {
     lock (LockKey)
     {
         return _variables[target].ContainsKey(variableName)
                    ? GetExisting(target, variableName)
                    : GetNew(target, variableName);
     }
 }
开发者ID:BADF00D,项目名称:Tanpohp,代码行数:9,代码来源:EnvironmentVariableProvider.cs

示例7: LoadEnvironmentVariables

        /// <summary>
        /// Loads the environment variables.
        /// </summary>
        /// <param name="dg">The Data Grid View.</param>
        /// <param name="target">The target.</param>
        private void LoadEnvironmentVariables(
            DataGrid dg, EnvironmentVariableTarget target)
        {
            dg.IsReadOnly = true;

            IDictionary environmentVariables
                = this.variableManger.GetEnvVariables(target);

            dg.ItemsSource = environmentVariables;
        }
开发者ID:blastmann,项目名称:EnvManWPF,代码行数:15,代码来源:MainWindow.xaml.cs

示例8: GetEnvironmentVariable

        public static string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
        {
            if (variable == null)
            {
                throw new ArgumentNullException(nameof(variable));
            }

            ValidateTarget(target);

            return GetEnvironmentVariableCore(variable, target);
        }
开发者ID:chcosta,项目名称:corefx,代码行数:11,代码来源:Environment.Uap.cs

示例9: Exists

        public bool Exists(string variableName, EnvironmentVariableTarget target)
        {
            lock (LockKey)
            {
                if (_variables[target].ContainsKey(variableName) && _variables[target][variableName].Content != null) return true;

                var variable = System.Environment.GetEnvironmentVariable(variableName, target);

                return variable != null;
            }
        }
开发者ID:BADF00D,项目名称:Tanpohp,代码行数:11,代码来源:EnvironmentVariableProvider.cs

示例10: Run

        /// <summary>
        /// This program allows you to view and modify the PATH environment.
        /// </summary>
        /// <param name="args"></param>
        private void Run(string[] args)
        {
            Console.OutputEncoding = Encoding.GetEncoding(Encoding.Default.CodePage);
            Args = new InputArgs("pathed", string.Format(resource.IDS_TITLE, AppVersion.Get()) + "\r\n" + resource.IDS_COPYRIGHT);

            Args.Add(InputArgType.Flag, "machine", false, Presence.Optional, resource.IDS_CMD_machine_doc);
            Args.Add(InputArgType.Flag, "user", false, Presence.Optional, resource.IDS_CMD_user_doc);
            Args.Add(InputArgType.ExistingDirectory, "add", "", Presence.Optional, resource.IDS_CMD_add_doc);
            Args.Add(InputArgType.ExistingDirectory, "append", "", Presence.Optional, resource.IDS_CMD_append_doc);
            Args.Add(InputArgType.StringList, "remove", null, Presence.Optional, resource.IDS_CMD_remove_doc);
            Args.Add(InputArgType.Flag, "slim", false, Presence.Optional, resource.IDS_CMD_slim_doc);
            Args.Add(InputArgType.Parameter, "env", "PATH", Presence.Optional, resource.IDS_CMD_env_doc);

            if (Args.Process(args))
            {
                EnvironmentVariableName = Args.GetString("env");

                if (Args.GetFlag("slim"))
                    SlimPath();

                if (Args.GetFlag("machine"))
                    EnvironmentVariableTarget = EnvironmentVariableTarget.Machine;

                else if (Args.GetFlag("user"))
                    EnvironmentVariableTarget = EnvironmentVariableTarget.User;

                try
                {
                    List<string> removeItems = Args.GetStringList("remove");
                    if (removeItems != null)
                        Remove(removeItems);

                    string add = Args.GetString("add");
                    if (!string.IsNullOrEmpty(add))
                        AddHead(SanitizePath(add));

                    string append = Args.GetString("append");
                    if (!string.IsNullOrEmpty(append))
                        AddTail(SanitizePath(append));
                }
                catch (SecurityException ex)
                {
                    if (EnvironmentVariableTarget == EnvironmentVariableTarget.Machine)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(resource.IDS_ERR_access_denied);
                        return;
                    }
                    else throw;
                }

                ListPath();
            }
        }
开发者ID:nathansgreen,项目名称:pserv4,代码行数:58,代码来源:pathed.cs

示例11: LoadEnvironment

        public void LoadEnvironment(EnvironmentVariableTarget target)
        {
            VarList.Items.Clear();

            environment = new EnvModel(target);

            foreach (string variable in environment.Variables.Keys)
            {
                VarList.Items.Add(variable);
            }

            SelectVariable(DefaultVariable);
        }
开发者ID:gemerson,项目名称:EEdit,代码行数:13,代码来源:EnvEditor.cs

示例12: LoadEnvironment

        public void LoadEnvironment(EnvironmentVariableTarget target)
        {
            VarList.Items.Clear();

            environment = new EnvModel(target);

            foreach (string variable in environment.Variables.Keys)
            {
                VarList.Items.Add(variable);
                environment.Variables[variable].CollectionChanged += new NotifyCollectionChangedEventHandler(Entries_CollectionChanged);
            }

            SelectVariable(DefaultVariable);
        }
开发者ID:Mirozell,项目名称:EEdit,代码行数:14,代码来源:EnvEditor.cs

示例13: GetVariable

        public static string GetVariable(string variable, EnvironmentVariableTarget target = EnvironmentVariableTarget.User)
        {
            string value;
            try
            {
                value = Environment.GetEnvironmentVariable(variable, target);
            }
            catch
            {
                value = null;
            }

            return value;
        }
开发者ID:justinlhudson,项目名称:Impromptu,代码行数:14,代码来源:EnvironmentVariable.cs

示例14: EditModel

 /// <summary>
 /// Initializes a new instance of the <see cref="EditModel" /> class.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="value">The value.</param>
 /// <param name="target">The target.</param>
 /// <param name="canEditName">if set to <c> true </c> [can edit name].</param>
 /// <param name="canEditValue">if set to <c> true </c> [can edit value].</param>
 /// <param name="canEditTarget">if set to <c> true </c> [can edit target].</param>
 public EditModel(
     string name = EmptyString,
     string value = EmptyString,
     EnvironmentVariableTarget target = EnvironmentVariableTarget.Process,
     bool canEditName = true,
     bool canEditValue = true,
     bool canEditTarget = true)
 {
     this.CanEditName = canEditName;
     this.CanEditValue = canEditValue;
     this.CanEditTarget = canEditTarget;
     this.Value = value;
     this.Name = name;
     this.Target = target;
 }
开发者ID:AntonioColino,项目名称:EnvVarCleaner,代码行数:24,代码来源:EditModel.cs

示例15: Parse

        public bool Parse(string[] args)
        {
            Target = EnvironmentVariableTarget.Machine;

            foreach (string arg in args)
            {
                if (arg.StartsWith("-"))
                {
                    switch (arg)
                    {
                        case "-p":
                            Target = EnvironmentVariableTarget.Process;
                            break;

                        case "-m":
                            Target = EnvironmentVariableTarget.Machine;
                            break;

                        case "-u":
                            Target = EnvironmentVariableTarget.User;
                            break;

                        default:
                            Console.WriteLine("Unknown option: {0}", arg);
                            return false;
                    }
                }
                else
                {
                    if (PathSegment == null)
                    {
                        PathSegment = arg;
                    }
                    else
                    {
                        Console.WriteLine("Unknown parameter: {0}", arg);
                        return false;
                    }
                }

            }

            if (PathSegment == null)
            {
                return false;
            }
            return true;
        }
开发者ID:JasonRowe,项目名称:AddPath,代码行数:48,代码来源:Options.cs


注:本文中的EnvironmentVariableTarget类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。