本文整理汇总了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;
}
}
}
示例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;
}
示例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();
}
示例4: GetExisting
private string GetExisting(EnvironmentVariableTarget target, string variableName)
{
var existingVariable = _variables[target][variableName];
if (!IsValid(existingVariable)) UpdateVariable(existingVariable);
return existingVariable.Content;
}
示例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);
}
}
示例6: Get
public string Get(string variableName, EnvironmentVariableTarget target)
{
lock (LockKey)
{
return _variables[target].ContainsKey(variableName)
? GetExisting(target, variableName)
: GetNew(target, variableName);
}
}
示例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;
}
示例8: GetEnvironmentVariable
public static string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target)
{
if (variable == null)
{
throw new ArgumentNullException(nameof(variable));
}
ValidateTarget(target);
return GetEnvironmentVariableCore(variable, target);
}
示例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;
}
}
示例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();
}
}
示例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);
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}