Ejemplo n.º 1
0
        /// <summary>
        /// Exports the environment variable to the chosen text file.
        /// </summary>
        /// <param name="setting">The setting.</param>
        public static void ExportEnvVar(EnvVarSetting setting)
        {
            if (setting == null)
            {
                HandleError(UIText.NoEnvSelected);
                return;
            }

            var desktop         = Environment.SpecialFolder.DesktopDirectory.ToString();
            var saveFileDialog1 = new SaveFileDialog
            {
                Filter           = UIText.SaveFileFilter,
                Title            = UIText.SaveFileTitle,
                InitialDirectory = desktop,
                FileName         = $"{setting.Name}.{UIText.SaveFileExt}",
                FilterIndex      = 2,
                RestoreDirectory = true
            };

            if (saveFileDialog1.ShowDialog() == null)
            {
                return;
            }
            if (saveFileDialog1.FileName == UIText.EmptyString)
            {
                return;
            }
            File.WriteAllText(saveFileDialog1.FileName, setting.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Cleans the environment variable.
        /// </summary>
        /// <param name="setting">The setting.</param>
        /// <param name="useShortPaths">if set to <c>true</c> [use short paths].</param>
        /// <param name="deleteNonPath">if set to <c>true</c> [delete non path].</param>
        public static void CleanEnvVar(EnvVarSetting setting, bool useShortPaths, bool deleteNonPath)
        {
            if (setting == null)
            {
                HandleError(UIText.NoEnvSelected);
                return;
            }

            var environmentVariableValue = setting.Value;

            if (environmentVariableValue != null)
            {
                var list =
                    new List <string>(
                        environmentVariableValue.Split(
                            new[] { UIText.Delimitter },
                            StringSplitOptions.RemoveEmptyEntries));
                var varBuilder = new StringBuilder(UIText.MaxPath);
                foreach (var str in list)
                {
                    if (!deleteNonPath || PathExists(str))
                    {
                        var shortname = useShortPaths
                                            ? PathName.GetShortPathName(str)
                                            : PathName.GetLongPathName(str);
                        varBuilder.Append(shortname);
                        varBuilder.Append(UIText.Delimitter);
                    }
                    else
                    {
                        if (!App.Verbose)
                        {
                            continue;
                        }
                        var builder = new StringBuilder();
                        builder.Append(UIText.EnvVarEntry);
                        builder.Append(str);
                        builder.Append(UIText.DiscardPath);
                        var result = MessageBox.Show(
                            builder.ToString(),
                            App.AppName,
                            MessageBoxButton.YesNoCancel,
                            MessageBoxImage.Warning);
                        if (result == MessageBoxResult.OK || result == MessageBoxResult.Yes)
                        {
                            continue;
                        }
                        var shortname = useShortPaths
                                            ? PathName.GetShortPathName(str)
                                            : PathName.GetLongPathName(str);
                        varBuilder.Append(shortname);
                        varBuilder.Append(UIText.Delimitter);
                    }
                }

                setting.Value = varBuilder.ToString();
                SetEnvVar(setting);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Determines whether the specified setting is an existing enviornment variable.
 /// </summary>
 /// <param name="setting">The setting.</param>
 /// <returns><c> true </c> if [is existing enviornment variable] [the specified setting]; otherwise, <c> false </c>.</returns>
 public static bool IsExistingEnvVar(EnvVarSetting setting)
 {
     if (setting == null)
     {
         return(false);
     }
     return(GetEnvironmentVariables().Any(envVarSetting => string.Equals(setting.Name, envVarSetting.Name)));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Deletes the environment variable completely.
 /// </summary>
 /// <param name="setting">The setting.</param>
 public static void DeleteEnvVar(EnvVarSetting setting)
 {
     if (setting == null)
     {
         HandleError(UIText.NoEnvSelected);
         return;
     }
     Environment.SetEnvironmentVariable(setting.Name, null, setting.Target);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Cleans the environment variable.
        /// </summary>
        /// <param name="setting">The setting.</param>
        /// <param name="useShortPaths">if set to <c>true</c> [use short paths].</param>
        /// <param name="deleteNonPath">if set to <c>true</c> [delete non path].</param>
        public static void CleanEnvVar(EnvVarSetting setting, bool useShortPaths, bool deleteNonPath)
        {
            if (setting == null)
            {
                HandleError(UIText.NoEnvSelected);
                return;
            }

            var environmentVariableValue = setting.Value;
            if (environmentVariableValue != null)
            {
                var list =
                    new List<string>(
                        environmentVariableValue.Split(
                            new[] { UIText.Delimitter },
                            StringSplitOptions.RemoveEmptyEntries));
                var varBuilder = new StringBuilder(UIText.MaxPath);
                foreach (var str in list)
                {
                    if (!deleteNonPath || PathExists(str))
                    {
                        var shortname = useShortPaths
                                            ? PathName.GetShortPathName(str)
                                            : PathName.GetLongPathName(str);
                        varBuilder.Append(shortname);
                        varBuilder.Append(UIText.Delimitter);
                    }
                    else {
                        if (!App.Verbose) { continue; }
                        var builder = new StringBuilder();
                        builder.Append(UIText.EnvVarEntry);
                        builder.Append(str);
                        builder.Append(UIText.DiscardPath);
                        var result = MessageBox.Show(
                            builder.ToString(),
                            App.AppName,
                            MessageBoxButton.YesNoCancel,
                            MessageBoxImage.Warning);
                        if (result == MessageBoxResult.OK || result == MessageBoxResult.Yes) { continue; }
                        var shortname = useShortPaths
                                            ? PathName.GetShortPathName(str)
                                            : PathName.GetLongPathName(str);
                        varBuilder.Append(shortname);
                        varBuilder.Append(UIText.Delimitter);
                    }
                }

                setting.Value = varBuilder.ToString();
                SetEnvVar(setting);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a new setting From the string.
        /// </summary>
        /// <param name="envVarString">The environment variable string.</param>
        /// <returns>EnvVarSetting.</returns>
        public static EnvVarSetting FromString(string envVarString)
        {
            var other      = new EnvVarSetting();
            var delimiters = new[] { ',' };
            var split      = new List <string>(envVarString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries));
            var myXName    = string.Empty;
            var myXValue   = string.Empty;
            var myXTarget  = string.Empty;

            foreach (var strVar in split)
            {
                if (strVar.Contains(UIText.XName))
                {
                    myXName = strVar.Remove(0, UIText.XName.Length);
                }
                else
                {
                    if (strVar.Contains(UIText.XValue))
                    {
                        myXValue = strVar.Remove(0, UIText.XValue.Length);
                    }
                    else
                    {
                        if (strVar.Contains(UIText.XTarget))
                        {
                            myXTarget = strVar.Remove(0, UIText.XTarget.Length);
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(myXName) || string.IsNullOrEmpty(myXValue))
            {
                return(null);
            }
            other.Name  = myXName;
            other.Value = myXValue;
            if (string.IsNullOrEmpty(myXTarget))
            {
                return(other);
            }
            EnvironmentVariableTarget target;

            Enum.TryParse(myXTarget, true, out target);
            other.Target = target;
            return(other);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Shows the specified setting.
        /// </summary>
        /// <param name="setting">The setting.</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>
        /// <returns>EnvVarSetting.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static KeyValuePair <EnvVarSetting, MessageBoxResult> Show(
            EnvVarSetting setting,
            bool canEditName   = true,
            bool canEditValue  = true,
            bool canEditTarget = true)
        {
            var w = new EditWindow();

            w.Update(setting, canEditName, canEditValue, canEditTarget);
            var dialog = w.ShowDialog();

            if (dialog == null)
            {
                throw new ArgumentNullException(nameof(dialog));
            }

            return(new KeyValuePair <EnvVarSetting, MessageBoxResult>(Setting, Result));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Sets the enviornment variable.
        /// </summary>
        /// <param name="setting">The setting.</param>
        public static void SetEnvVar(EnvVarSetting setting)
        {
            if (setting == null)
            {
                HandleError(UIText.NoEnvSelected);
                return;
            }

            if (!IsExistingEnvVar(setting) || !App.Verbose)
            {
                // If the variable does not exist or the app does not have tight checking,
                // we can write it without worry that it will overwrite something.
                Environment.SetEnvironmentVariable(setting.Name, setting.Value, setting.Target);
            }
            else
            {
                // If the variable does exist than we have to check.
                var builder = new StringBuilder();
                builder.Append(UIText.AreYouSureOw);
                builder.Append(setting.Name);
                builder.Append(UIText.AreYouSureWv);
                builder.Append(setting.Value);
                builder.Append(UIText.AreYouSureIt);
                builder.Append(setting.Target);
                builder.Append(UIText.AreYouSureG);
                var result = MessageBox.Show(
                    builder.ToString(),
                    App.AppName,
                    MessageBoxButton.YesNoCancel,
                    MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK || result == MessageBoxResult.Yes)
                {
                    // if it is approved...
                    Environment.SetEnvironmentVariable(setting.Name, setting.Value, setting.Target);
                }
                else
                {
                    HandleMsg(UIText.NoChanges);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Updates the specified setting.
        /// </summary>
        /// <param name="setting">The setting.</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>
        private void Update(
            EnvVarSetting setting = null,
            bool canEditName      = true,
            bool canEditValue     = true,
            bool canEditTarget    = true)
        {
            if (setting != null)
            {
                Setting = setting;
            }
            else
            {
                setting = Setting ?? EnvVarSetting.DefaultSetting;
            }

            EditModel               = setting;
            EditModel.CanEditName   = canEditName;
            EditModel.CanEditValue  = canEditValue;
            EditModel.CanEditTarget = canEditTarget;
            DataContext             = EditModel;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Sends a confirm question to the ui to check if the setting is correct.
        /// If it is, it fires the given delegate.
        /// The function must match the signature, public void MyFunction(EnvVarSetting setting); .
        /// </summary>
        /// <param name="actionMsg">The action MSG.</param>
        /// <param name="setting">The setting.</param>
        /// <param name="function">The function.</param>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public static void QuestionSetting(string actionMsg, EnvVarSetting setting, EnvVarSettingFunction function)
        {
            var builder = new StringBuilder();

            builder.Append(UIText.AreYouSure);
            builder.Append(actionMsg);
            builder.Append(UIText.EnvVarXName);
            builder.Append(setting.Name);
            builder.Append(UIText.Comma);
            builder.Append(UIText.XValue);
            builder.Append(setting.Value);
            builder.Append(UIText.Comma);
            builder.Append(UIText.XTarget);
            builder.Append(setting.Target);
            builder.Append(UIText.EndCap);
            var result = MessageBox.Show(
                builder.ToString(),
                App.AppName,
                MessageBoxButton.YesNoCancel,
                MessageBoxImage.Warning);

            switch (result)
            {
            case MessageBoxResult.OK:
            case MessageBoxResult.Yes:
                function(setting);
                break;

            case MessageBoxResult.None:
            case MessageBoxResult.Cancel:
            case MessageBoxResult.No:
                break;

            default:
                throw new ArgumentOutOfRangeException(UIText.MboxRangeError);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Imports the environment variable.
        /// </summary>
        /// <param name="setting">The setting.</param>
        public static void ImportEnvVar(EnvVarSetting setting)
        {
            if (setting == null)
            {
                setting = EnvVarSetting.DefaultSetting;
            }
            var desktop         = Environment.SpecialFolder.DesktopDirectory.ToString();
            var saveFileDialog1 = new OpenFileDialog
            {
                Filter           = UIText.OpenFileFilter,
                Title            = UIText.OpenFileTitle,
                InitialDirectory = desktop,
                FileName         = $"{setting.Name}.{UIText.OpenFileExt}",
                FilterIndex      = 2,
                RestoreDirectory = true
            };

            if (saveFileDialog1.ShowDialog() == null)
            {
                return;
            }
            if (saveFileDialog1.FileName == UIText.EmptyString)
            {
                return;
            }
            var allText = File.ReadAllText(saveFileDialog1.FileName);

            setting = EnvVarSetting.FromString(allText);
            if (setting != null)
            {
                SetEnvVar(setting);
            }
            else
            {
                HandleError(UIText.ParseError);
            }
        }
Ejemplo n.º 12
0
 // protected methods...
 /// <summary>
 /// Equals the specified other.
 /// </summary>
 /// <param name="other">The other.</param>
 /// <returns><c> true </c> if XXXX, <c> false </c> otherwise.</returns>
 protected bool Equals(EnvVarSetting other)
 {
     return string.Equals(name, other.name) && string.Equals(value, other.value) && Target == other.Target;
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Imports the environment variable.
 /// </summary>
 /// <param name="setting">The setting.</param>
 public static void ImportEnvVar(EnvVarSetting setting)
 {
     if (setting == null) { setting = EnvVarSetting.DefaultSetting; }
     var desktop = Environment.SpecialFolder.DesktopDirectory.ToString();
     var saveFileDialog1 = new OpenFileDialog
     {
         Filter = UIText.OpenFileFilter,
         Title = UIText.OpenFileTitle,
         InitialDirectory = desktop,
         FileName = $"{setting.Name}.{UIText.OpenFileExt}",
         FilterIndex = 2,
         RestoreDirectory = true
     };
     if (saveFileDialog1.ShowDialog() == null) { return; }
     if (saveFileDialog1.FileName == UIText.EmptyString) { return; }
     var allText = File.ReadAllText(saveFileDialog1.FileName);
     setting = EnvVarSetting.FromString(allText);
     if (setting != null)
     {
         SetEnvVar(setting);
     }
     else {
         HandleError(UIText.ParseError);
     }
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Determines whether the specified setting is an existing enviornment variable.
 /// </summary>
 /// <param name="setting">The setting.</param>
 /// <returns><c> true </c> if [is existing enviornment variable] [the specified setting]; otherwise, <c> false </c>.</returns>
 public static bool IsExistingEnvVar(EnvVarSetting setting)
 {
     if (setting == null) { return false; }
     return GetEnvironmentVariables().Any(envVarSetting => string.Equals(setting.Name, envVarSetting.Name));
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Sends a confirm question to the ui to check if the setting is correct.
        /// If it is, it fires the given delegate.
        /// The function must match the signature, public void MyFunction(EnvVarSetting setting); .
        /// </summary>
        /// <param name="actionMsg">The action MSG.</param>
        /// <param name="setting">The setting.</param>
        /// <param name="function">The function.</param>
        /// <exception cref="System.ArgumentOutOfRangeException"></exception>
        public static void QuestionSetting(string actionMsg, EnvVarSetting setting, EnvVarSettingFunction function)
        {
            var builder = new StringBuilder();
            builder.Append(UIText.AreYouSure);
            builder.Append(actionMsg);
            builder.Append(UIText.EnvVarXName);
            builder.Append(setting.Name);
            builder.Append(UIText.Comma);
            builder.Append(UIText.XValue);
            builder.Append(setting.Value);
            builder.Append(UIText.Comma);
            builder.Append(UIText.XTarget);
            builder.Append(setting.Target);
            builder.Append(UIText.EndCap);
            var result = MessageBox.Show(
                builder.ToString(),
                App.AppName,
                MessageBoxButton.YesNoCancel,
                MessageBoxImage.Warning);
            switch (result)
            {
                case MessageBoxResult.OK:
                case MessageBoxResult.Yes:
                    function(setting);
                    break;

                case MessageBoxResult.None:
                case MessageBoxResult.Cancel:
                case MessageBoxResult.No:
                    break;

                default:
                    throw new ArgumentOutOfRangeException(UIText.MboxRangeError);
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Shows the specified setting.
        /// </summary>
        /// <param name="setting">The setting.</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>
        /// <returns>EnvVarSetting.</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static KeyValuePair<EnvVarSetting, MessageBoxResult> Show(
            EnvVarSetting setting,
            bool canEditName = true,
            bool canEditValue = true,
            bool canEditTarget = true)
        {
            var w = new EditWindow();
            w.Update(setting, canEditName, canEditValue, canEditTarget);
            var dialog = w.ShowDialog();
            if (dialog == null) { throw new ArgumentNullException(nameof(dialog)); }

            return new KeyValuePair<EnvVarSetting, MessageBoxResult>(Setting, Result);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Exports the environment variable to the chosen text file.
        /// </summary>
        /// <param name="setting">The setting.</param>
        public static void ExportEnvVar(EnvVarSetting setting)
        {
            if (setting == null)
            {
                HandleError(UIText.NoEnvSelected);
                return;
            }

            var desktop = Environment.SpecialFolder.DesktopDirectory.ToString();
            var saveFileDialog1 = new SaveFileDialog
            {
                Filter = UIText.SaveFileFilter,
                Title = UIText.SaveFileTitle,
                InitialDirectory = desktop,
                FileName = $"{setting.Name}.{UIText.SaveFileExt}",
                FilterIndex = 2,
                RestoreDirectory = true
            };
            if (saveFileDialog1.ShowDialog() == null) { return; }
            if (saveFileDialog1.FileName == UIText.EmptyString) { return; }
            File.WriteAllText(saveFileDialog1.FileName, setting.ToString());
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Creates a new setting From the string.
        /// </summary>
        /// <param name="envVarString">The environment variable string.</param>
        /// <returns>EnvVarSetting.</returns>
        public static EnvVarSetting FromString(string envVarString)
        {
            var other = new EnvVarSetting();
            var delimiters = new[] { ',' };
            var split = new List<string>(envVarString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries));
            var myXName = string.Empty;
            var myXValue = string.Empty;
            var myXTarget = string.Empty;
            foreach (var strVar in split)
            {
                if (strVar.Contains(UIText.XName))
                {
                    myXName = strVar.Remove(0, UIText.XName.Length);
                }
                else {
                    if (strVar.Contains(UIText.XValue))
                    {
                        myXValue = strVar.Remove(0, UIText.XValue.Length);
                    }
                    else {
                        if (strVar.Contains(UIText.XTarget)) { myXTarget = strVar.Remove(0, UIText.XTarget.Length); }
                    }
                }
            }

            if (string.IsNullOrEmpty(myXName) || string.IsNullOrEmpty(myXValue)) { return null; }
            other.Name = myXName;
            other.Value = myXValue;
            if (string.IsNullOrEmpty(myXTarget)) { return other; }
            EnvironmentVariableTarget target;
            Enum.TryParse(myXTarget, true, out target);
            other.Target = target;
            return other;
        }
Ejemplo n.º 19
0
 // protected methods...
 /// <summary>
 /// Equals the specified other.
 /// </summary>
 /// <param name="other">The other.</param>
 /// <returns><c> true </c> if XXXX, <c> false </c> otherwise.</returns>
 protected bool Equals(EnvVarSetting other)
 {
     return(string.Equals(name, other.name) && string.Equals(value, other.value) && Target == other.Target);
 }
Ejemplo n.º 20
0
        /// <summary>
        /// Sets the enviornment variable.
        /// </summary>
        /// <param name="setting">The setting.</param>
        public static void SetEnvVar(EnvVarSetting setting)
        {
            if (setting == null)
            {
                HandleError(UIText.NoEnvSelected);
                return;
            }

            if (!IsExistingEnvVar(setting) || !App.Verbose)
            {
                // If the variable does not exist or the app does not have tight checking,
                // we can write it without worry that it will overwrite something.
                Environment.SetEnvironmentVariable(setting.Name, setting.Value, setting.Target);
            }
            else {
                // If the variable does exist than we have to check.
                var builder = new StringBuilder();
                builder.Append(UIText.AreYouSureOw);
                builder.Append(setting.Name);
                builder.Append(UIText.AreYouSureWv);
                builder.Append(setting.Value);
                builder.Append(UIText.AreYouSureIt);
                builder.Append(setting.Target);
                builder.Append(UIText.AreYouSureG);
                var result = MessageBox.Show(
                    builder.ToString(),
                    App.AppName,
                    MessageBoxButton.YesNoCancel,
                    MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK || result == MessageBoxResult.Yes)
                {
                    // if it is approved...
                    Environment.SetEnvironmentVariable(setting.Name, setting.Value, setting.Target);
                }
                else {
                    HandleMsg(UIText.NoChanges);
                }
            }
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Updates the specified setting.
        /// </summary>
        /// <param name="setting">The setting.</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>
        private void Update(
            EnvVarSetting setting = null,
            bool canEditName = true,
            bool canEditValue = true,
            bool canEditTarget = true)
        {
            if (setting != null)
            {
                Setting = setting;
            }
            else {
                setting = Setting ?? EnvVarSetting.DefaultSetting;
            }

            EditModel = setting;
            EditModel.CanEditName = canEditName;
            EditModel.CanEditValue = canEditValue;
            EditModel.CanEditTarget = canEditTarget;
            DataContext = EditModel;
        }
Ejemplo n.º 22
0
 /// <summary>
 /// Deletes the environment variable completely.
 /// </summary>
 /// <param name="setting">The setting.</param>
 public static void DeleteEnvVar(EnvVarSetting setting)
 {
     if (setting == null)
     {
         HandleError(UIText.NoEnvSelected);
         return;
     }
     Environment.SetEnvironmentVariable(setting.Name, null, setting.Target);
 }