XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEToolBox

【WPF】XFE工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace XFEToolBox.QuickTools;

public sealed partial class QuickToolPage
{
    private void BuildPorts()
    {
        var port = Field("PortInput", Context.Text ?? ""); port.Width = 150; port.ToolTip = "输入端口号;留空列出全部";
        var protocol = Choices(["全部协议", "TCP", "UDP"]); var query = Field("PortSearch"); query.Width = 190; query.ToolTip = "按进程名称、PID、地址筛选";
        var rows = Table("PortResults", ("端口", "Port", .65), ("协议", "Protocol", .7), ("本地地址", "LocalAddress", 1.5), ("状态", "State", .8), ("进程", "Process.Name", 1.5), ("PID", "Process.Pid", .7));
        rows.Height = 265; var details = Field("PortDetails", "选择一行,查看所属进程和路径。", true, true); details.MinHeight = 100; details.Height = 105;
        var total = Label("—", 24); var processes = Label("—", 24); var listeners = Label("—", 24); IReadOnlyList<PortEntry> all = [];
        void Filter()
        {
            var selected = rows.SelectedItem as PortEntry; string q = query.Text.Trim();
            rows.ItemsSource = all.Where(r => (protocol.SelectedIndex == 0 || r.Protocol.StartsWith((string)protocol.SelectedItem, StringComparison.OrdinalIgnoreCase)) && (q.Length == 0 || r.Details.Contains(q, StringComparison.OrdinalIgnoreCase))).ToArray();
            if (selected is not null && rows.Items.Contains(selected)) rows.SelectedItem = selected;
        }
        async Task Scan()
        {
            int? value = string.IsNullOrWhiteSpace(port.Text) ? null : int.Parse(port.Text.Trim());
            all = await Task.Run(() => PortInspector.Query(value), cancellation.Token); Filter();
            total.Text = all.Count.ToString("N0"); processes.Text = all.Select(r => r.Process.Pid).Distinct().Count().ToString("N0"); listeners.Text = all.Count(r => r.State is "监听" or "绑定").ToString("N0");
            status.Text = $"已查询 {all.Count:N0} 个端点 · 可点击表头排序,选择一行查看程序位置";
        }
        query.TextChanged += (_, _) => Filter(); protocol.SelectionChanged += (_, _) => Filter();
        var reveal = ActionButton("打开程序位置", () => { if (rows.SelectedItem is PortEntry e) Reveal(e.Process.Path); });
        var terminate = ActionButton("结束选中进程", async () => { if (rows.SelectedItem is not PortEntry e) throw new InvalidOperationException("请先选择进程。"); if (!Confirm("结束进程?", e.Process.Description + "\n\n这将结束整个进程,可能丢失未保存的工作。")) { status.Text = "已取消。"; return; } await Task.Run(() => PortInspector.Terminate(e.Process)); await Scan(); });
        terminate.Foreground = System.Windows.Media.Brushes.IndianRed; reveal.IsEnabled = terminate.IsEnabled = false;
        rows.SelectionChanged += (_, _) => { var selected = rows.SelectedItem as PortEntry; details.Text = selected?.Details ?? "选择一行,查看所属进程和路径。"; reveal.IsEnabled = selected is not null && System.IO.File.Exists(selected.Process.Path); terminate.IsEnabled = selected is not null && selected.Process.Pid > 4 && selected.Process.Pid != Environment.ProcessId; };
        body.Children.Add(Actions(Label("端口"), port, Primary("查询 / 刷新", Scan), protocol, query));
        var metrics = new UniformGrid { Columns = 3 }; metrics.Children.Add(Metric("网络端点", total)); metrics.Children.Add(Metric("所属进程", processes)); metrics.Children.Add(Metric("监听 / 绑定", listeners)); body.Children.Add(metrics);
        body.Children.Add(rows); body.Children.Add(Section("进程检查器", details, Actions(reveal, terminate, Copy(details))));
        if (!string.IsNullOrWhiteSpace(Context.Text)) Loaded += async (_, _) => await Run(Scan);
    }
}