/// <summary> /// Deletes an existing file or directory. /// </summary> /// <param name="path"> /// The path to the file or directory to be deleted. /// </param> /// <param name="runAsAdmin"> /// <see langword="true"/> to run this task with administrator privileges; /// otherwise, <see langword="false"/>. /// </param> /// <param name="wait"> /// <see langword="true"/> to wait indefinitely for the associated process to /// exit; otherwise, <see langword="false"/>. /// </param> public static bool Delete(string path, bool runAsAdmin = false, bool wait = true) { if (string.IsNullOrWhiteSpace(path)) { return(false); } var src = PathEx.Combine(path); if (!PathEx.DirOrFileExists(src)) { return(true); } int exitCode; using (var p = Send((PathEx.IsDir(src) ? "RMDIR /S /Q \"{0}\"" : "DEL /F /Q \"{0}\"").FormatCurrent(src), runAsAdmin, false)) { if (!wait) { return(true); } if (p?.HasExited ?? false) { p.WaitForExit(); } exitCode = p?.ExitCode ?? 1; } return(exitCode == 0); }
/// <summary> /// Creates all directories and subdirectories in the specified path unless /// they already exist. /// </summary> /// <param name="path"> /// The directory to create. /// </param> public static bool Create(string path) { try { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } var dir = PathEx.Combine(path); if (Directory.Exists(dir)) { return(true); } if (File.Exists(dir) && !PathEx.IsDir(dir)) { File.Delete(dir); } var di = Directory.CreateDirectory(dir); return(di.Exists); } catch (Exception ex) when(ex.IsCaught()) { Log.Write(ex); return(false); } }
/// <summary> /// Extracts a large or small icon resource under the specified index, from the /// 'imageres.dll' under specified location, and returns its <see cref="Icon"/> /// instance. /// </summary> /// <param name="index"> /// The index of the icon to extract. /// </param> /// <param name="large"> /// <see langword="true"/> to return the large image; otherwise, /// <see langword="false"/>. /// </param> /// <param name="location"> /// The directory where the 'imageres.dll' file is located. /// </param> public static Icon GetSystemIcon(ImageResourceSymbol index, bool large = false, string location = "%system%") { try { var path = PathEx.Combine(location); if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentNullException(nameof(location)); } if (PathEx.IsDir(path)) { path = Path.Combine(path, "imageres.dll"); } if (!File.Exists(path)) { path = PathEx.Combine("%system%\\imageres.dll"); } return(GetIconFromFile(path, GetImageResourceValue(index), large)); } catch (Exception ex) when(ex.IsCaught()) { if (Log.DebugMode > 1) { Log.Write(ex); } } return(null); }
public static string GetImageResourceName(int value, string location = "%system%") { var path = PathEx.Combine(location); if (!string.IsNullOrWhiteSpace(path) && PathEx.IsDir(path)) { path = Path.Combine(path, "imageres.dll"); } if (!path.EndsWithEx("imageres.dll") || !File.Exists(path)) { path = PathEx.Combine("%system%\\imageres.dll"); } var version = FileEx.GetFileVersion(path); // Windows 10 if (version.Major >= 10) { return(Enum.GetName(typeof(ImageResourceSymbol), value)); } // Windows 7 + 8 if (value < 187) { return(Enum.GetName(typeof(ImageResourceSymbol), value)); } if (value.IsBetween(186, 214)) { return(Enum.GetName(typeof(ImageResourceSymbol), ++value)); } if (value.IsBetween(216, version.Minor < 2 ? 218 : 306)) { return(Enum.GetName(typeof(ImageResourceSymbol), value + (value < 240 ? 17 : 16))); } return(null); }
/// <summary> /// Waits for the specified seconds to delete the target at the specified path. /// </summary> /// <param name="path"> /// The path to the file or directory to be deleted. /// </param> /// <param name="seconds"> /// The time to wait in seconds. /// </param> /// <param name="runAsAdmin"> /// <see langword="true"/> to run this task with administrator privileges; /// otherwise, <see langword="false"/>. /// </param> /// <param name="dispose"> /// <see langword="true"/> to release all resources used by the /// <see cref="Process"/> component, if this task has been started; otherwise, /// <see langword="false"/>. /// </param> public static Process WaitThenDelete(string path, int seconds = 5, bool runAsAdmin = false, bool dispose = true) { if (string.IsNullOrWhiteSpace(path)) { return(null); } var src = PathEx.Combine(path); if (!PathEx.DirOrFileExists(src)) { return(null); } var time = seconds < 1 ? 1 : seconds > 3600 ? 3600 : seconds; var command = (PathEx.IsDir(src) ? "RMDIR /S /Q \"{0}\"" : "DEL /F /Q \"{0}\"").FormatCurrent(src); return(WaitThenCmd(command, time, runAsAdmin, dispose)); }
/// <summary> /// Retrieves a backward-compatible integer value of the specified /// <see cref="ImageResourceSymbol"/> value, which depends on the file version /// of the 'imageres.dll' file under the specified location. /// </summary> /// <param name="value"> /// The <see cref="ImageResourceSymbol"/> value. /// </param> /// <param name="location"> /// The directory where the 'imageres.dll' file is located. /// </param> public static int GetImageResourceValue(ImageResourceSymbol value, string location = "%system%") { var path = PathEx.Combine(location); if (!string.IsNullOrWhiteSpace(path) && PathEx.IsDir(path)) { path = Path.Combine(path, "imageres.dll"); } if (!path.EndsWithEx("imageres.dll") || !File.Exists(path)) { path = PathEx.Combine("%system%\\imageres.dll"); } var version = FileEx.GetFileVersion(path); // Windows 10 if (version.Major >= 10) { return((int)value); } // Windows 7 + 8 var index = (int)value; if (index < 187) { return(index); } if (index.IsBetween(187, 215)) { return(--index); } if (index.IsBetween(233, version.Minor < 2 ? 235 : 322)) { return(index - (index < 257 ? 17 : 16)); } return(-1); }
/// <summary> /// Initializes an instance of the <see cref="IconBrowserDialog"/> class. /// </summary> /// <param name="path"> /// The path of the file to open. /// </param> /// <param name="backColor"> /// The background color of the dialog box. /// </param> /// <param name="foreColor"> /// The foreground color of the dialog box. /// </param> /// <param name="buttonFace"> /// The button color of the dialog box. /// </param> /// <param name="buttonText"> /// The button text color of the dialog box. /// </param> /// <param name="buttonHighlight"> /// The button highlight color of the dialog box. /// </param> public IconBrowserDialog(string path = "%system%\\imageres.dll", Color?backColor = null, Color?foreColor = null, Color?buttonFace = null, Color?buttonText = null, Color?buttonHighlight = null) { _components = new Container(); SuspendLayout(); var resPath = PathEx.Combine(path); if (PathEx.IsDir(resPath)) { resPath = PathEx.Combine(path, "imageres.dll"); } if (!File.Exists(resPath)) { resPath = PathEx.Combine("%system%", "imageres.dll"); } var resLoc = Path.GetDirectoryName(resPath); AutoScaleDimensions = new SizeF(96f, 96f); AutoScaleMode = AutoScaleMode.Dpi; BackColor = backColor ?? SystemColors.Control; ForeColor = foreColor ?? SystemColors.ControlText; Font = new Font("Consolas", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0); Icon = GetSystemIcon(IconIndex.DirectorySearch, true, resLoc); MaximizeBox = false; MaximumSize = new Size(680, Screen.FromHandle(Handle).WorkingArea.Height); MinimizeBox = false; MinimumSize = new Size(680, 448); Name = "IconBrowserForm"; Size = MinimumSize; SizeGripStyle = SizeGripStyle.Hide; StartPosition = FormStartPosition.CenterScreen; Text = UIStrings.ResourceBrowser; _tableLayoutPanel = new TableLayoutPanel { BackColor = Color.Transparent, CellBorderStyle = TableLayoutPanelCellBorderStyle.None, Dock = DockStyle.Fill, Name = "tableLayoutPanel", RowCount = 2 }; _tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 100f)); _tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 36)); Controls.Add(_tableLayoutPanel); _panel = new Panel { AutoScroll = true, BackColor = buttonFace ?? SystemColors.ButtonFace, BorderStyle = BorderStyle.FixedSingle, Enabled = false, ForeColor = buttonText ?? SystemColors.ControlText, Dock = DockStyle.Fill, Name = "panel", TabIndex = 0 }; _panel.Scroll += (s, e) => (s as Panel)?.Update(); _tableLayoutPanel.Controls.Add(_panel, 0, 0); _innerTableLayoutPanel = new TableLayoutPanel { BackColor = Color.Transparent, ColumnCount = 2, CellBorderStyle = TableLayoutPanelCellBorderStyle.None, Dock = DockStyle.Fill, Name = "innerTableLayoutPanel" }; _innerTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f)); _innerTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 24)); _tableLayoutPanel.Controls.Add(_innerTableLayoutPanel, 0, 1); _textBox = new TextBox { BorderStyle = BorderStyle.FixedSingle, Dock = DockStyle.Top, Font = Font, Name = "textBox", TabIndex = 1 }; _textBox.TextChanged += TextBox_TextChanged; _innerTableLayoutPanel.Controls.Add(_textBox, 0, 0); _buttonPanel = new Panel { Anchor = AnchorStyles.Top | AnchorStyles.Right, BackColor = Color.Transparent, BorderStyle = BorderStyle.FixedSingle, Name = "buttonPanel", Size = new Size(20, 20) }; _innerTableLayoutPanel.Controls.Add(_buttonPanel, 1, 0); _button = new Button { BackColor = buttonFace ?? SystemColors.ButtonFace, BackgroundImage = GetSystemIcon(IconIndex.Directory, false, resLoc).ToBitmap(), BackgroundImageLayout = ImageLayout.Zoom, Dock = DockStyle.Fill, FlatStyle = FlatStyle.Flat, Font = Font, ForeColor = buttonText ?? SystemColors.ControlText, Name = "button", TabIndex = 2, UseVisualStyleBackColor = false }; _button.FlatAppearance.BorderSize = 0; _button.FlatAppearance.MouseOverBackColor = buttonHighlight ?? ProfessionalColors.ButtonSelectedHighlight; _button.Click += Button_Click; _buttonPanel.Controls.Add(_button); _progressCircle = new ProgressCircle { Active = false, Anchor = AnchorStyles.Top | AnchorStyles.Right, BackColor = Color.Transparent, Dock = DockStyle.Fill, ForeColor = (backColor ?? SystemColors.Control).InvertRgb().ToGrayScale(), RotationSpeed = 80, Thickness = 2, Visible = true }; _timer = new Timer(_components) { Interval = 1 }; _timer.Tick += Timer_Tick; _iconBoxes = new List <IconBox>(); Shown += (sender, args) => TaskBarProgress.SetState(Handle, TaskBarProgressState.Indeterminate); ResumeLayout(false); PerformLayout(); var curPath = PathEx.Combine(path); if (!File.Exists(curPath)) { curPath = resPath; } if (!File.Exists(curPath)) { return; } _textBox.Text = curPath; }