SettingPanelHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Setting Panel Helper
  2. //
  3. // Douglas Thrift
  4. //
  5. // SettingPanelHelper.cs
  6. /* Copyright 2014 Douglas Thrift
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using System.Diagnostics;
  22. using System.IO;
  23. using System.Linq;
  24. using System.Security;
  25. using System.Windows.Controls;
  26. public static class SettingPanelHelper
  27. {
  28. public static void DoExecutableBrowseClick(AutoCompleteBox executableBox)
  29. {
  30. var dialog = new System.Windows.Forms.OpenFileDialog();
  31. dialog.AutoUpgradeEnabled = true;
  32. string pathext = Environment.GetEnvironmentVariable("PathExt");
  33. if (pathext == null)
  34. pathext = ".com;.exe;.bat;.cmd";
  35. pathext = string.Join(";", pathext.Split(';').Select(extension => "*" + extension.ToLowerInvariant()));
  36. dialog.Filter = string.Format("Executable files ({0})|{0}|All files (*.*)|*.*", pathext);
  37. dialog.FilterIndex = 1;
  38. dialog.RestoreDirectory = true;
  39. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  40. executableBox.Text = dialog.FileName;
  41. }
  42. public static void DoExecutableBoxPopulating(AutoCompleteBox executableBox)
  43. {
  44. try
  45. {
  46. string directoryName = Path.GetDirectoryName(executableBox.Text);
  47. if (directoryName == null)
  48. directoryName = Path.GetPathRoot(executableBox.Text);
  49. if (Directory.Exists(directoryName))
  50. {
  51. var directoryInfo = new DirectoryInfo(directoryName.EndsWith(":") ? directoryName + @"\" : directoryName);
  52. executableBox.ItemsSource = directoryInfo.EnumerateFileSystemInfos().Where(info => !info.Attributes.HasFlag(FileAttributes.Hidden)).Select(info => info.FullName);
  53. executableBox.PopulateComplete();
  54. }
  55. }
  56. catch (ArgumentException exception)
  57. {
  58. Debug.WriteLine("{0}: {1}", exception, exception.Message);
  59. }
  60. catch (PathTooLongException exception)
  61. {
  62. Debug.WriteLine("{0}: {1}", exception, exception.Message);
  63. }
  64. catch (SecurityException exception)
  65. {
  66. Debug.WriteLine("{0}: {1}", exception, exception.Message);
  67. }
  68. }
  69. public static void DoDirectoryBrowseClick(AutoCompleteBox directoryBox)
  70. {
  71. var dialog = new System.Windows.Forms.FolderBrowserDialog();
  72. dialog.ShowNewFolderButton = false;
  73. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  74. directoryBox.Text = dialog.SelectedPath;
  75. }
  76. public static void DoDirectoryBoxPopulating(AutoCompleteBox directoryBox)
  77. {
  78. try
  79. {
  80. string directoryName = Path.GetDirectoryName(directoryBox.Text);
  81. if (directoryName == null)
  82. directoryName = Path.GetPathRoot(directoryBox.Text);
  83. if (Directory.Exists(directoryName))
  84. {
  85. var directoryInfo = new DirectoryInfo(directoryName.EndsWith(":") ? directoryName + @"\" : directoryName);
  86. directoryBox.ItemsSource = directoryInfo.EnumerateDirectories().Where(info => !info.Attributes.HasFlag(FileAttributes.Hidden)).Select(info => info.FullName);
  87. directoryBox.PopulateComplete();
  88. }
  89. }
  90. catch (ArgumentException exception)
  91. {
  92. Debug.WriteLine("{0}: {1}", exception, exception.Message);
  93. }
  94. catch (PathTooLongException exception)
  95. {
  96. Debug.WriteLine("{0}: {1}", exception, exception.Message);
  97. }
  98. catch (SecurityException exception)
  99. {
  100. Debug.WriteLine("{0}: {1}", exception, exception.Message);
  101. }
  102. }
  103. }