【无标题】

C#自动防监控软件工具


一、演示效果

二、核心程序

c 复制代码
// Program.cs ------ 程序入口
// 目标框架:.NET Framework 4.8 / C# 5
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace MonitorGuard
{
    internal static class Program
    {
        /// <summary>数据目录:优先 exe 所在目录,不可写则退回 %LOCALAPPDATA%\MonitorGuard。</summary>
        public static string ResolveDataDir()
        {
            string exeDir = "";
            try { exeDir = Path.GetDirectoryName(Application.ExecutablePath); }
            catch { }

            if (!string.IsNullOrEmpty(exeDir))
            {
                try
                {
                    string probe = Path.Combine(exeDir, ".mg_write_test");
                    File.WriteAllText(probe, "ok");
                    File.Delete(probe);
                    return exeDir;
                }
                catch { }
            }

            string fallback = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), TrayContext.AppName);
            try { Directory.CreateDirectory(fallback); }
            catch { }
            return fallback;
        }

        [STAThread]
        private static void Main(string[] args)
        {
            // ---- 自检模式:扫描一次并输出报告,不驻留后台 ----
            if (args.Length > 0)
            {
                string a = args[0].ToLowerInvariant();
                if (a == "--scan" || a == "-scan" || a == "/scan")
                {
                    RunOnceScan();
                    return;
                }
            }

            // ---- 单实例 ----
            bool createdNew = false;
            Mutex mutex = null;
            try
            {
                mutex = new Mutex(true, "MonitorGuard_SingleInstance_v1", out createdNew);
            }
            catch
            {
                createdNew = true;
            }

            if (!createdNew)
            {
                MessageBox.Show("MonitorGuard 已经在后台运行了。\n\n请在系统托盘(右下角通知区域)中找到盾牌图标。",
                    "MonitorGuard", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            // ---- 全局异常兜底 ----
            Application.ThreadException += delegate(object s, ThreadExceptionEventArgs e)
            {
                WriteCrashLog(e.Exception);
            };
            AppDomain.CurrentDomain.UnhandledException += delegate(object s, UnhandledExceptionEventArgs e)
            {
                WriteCrashLog(e.ExceptionObject as Exception);
            };

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool openStatus = false;
            if (args.Length > 0)
            {
                string opt = args[0].ToLowerInvariant();
                if (opt == "--status" || opt == "-status" || opt == "/status") openStatus = true;
            }

            try
            {
                TrayContext ctx = new TrayContext();
                if (openStatus) ctx.ShowStatusWindow();
                Application.Run(ctx);
            }
            catch (Exception ex)
            {
                WriteCrashLog(ex);
                MessageBox.Show("MonitorGuard 运行出错:\n" + ex.Message, "MonitorGuard",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (mutex != null)
                {
                    try { mutex.ReleaseMutex(); }
                    catch { }
                    try { mutex.Dispose(); }
                    catch { }
                }
            }
        }

        /// <summary>执行一次扫描并把结果写到 scan-report.txt。</summary>
        private static void RunOnceScan()
        {
            string dir = ResolveDataDir();
            Logger log = new Logger(dir);
            Config cfg = Config.Load(Path.Combine(dir, "config.ini"));

            // 自检时不做基线跳过,直接看当前状态
            ScanEngine engine = new ScanEngine(cfg, log);
            ScanResult r = engine.Scan();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("MonitorGuard 一次性扫描报告");
            sb.AppendLine("时间   : " + r.FinishedAt.ToString("yyyy-MM-dd HH:mm:ss"));
            sb.AppendLine("耗时   : " + r.ElapsedMs + " ms");
            sb.AppendLine("异常项 : " + r.Threats.Count);
            sb.AppendLine(new string('-', 70));
            if (r.Threats.Count == 0)
            {
                sb.AppendLine("未发现监控 / 截屏 / 录屏迹象。");
            }
            for (int i = 0; i < r.Threats.Count; i++)
            {
                Threat t = r.Threats[i];
                sb.AppendLine("[" + t.LevelText + "][" + t.Category + "] " + t.Title);
                sb.AppendLine("    说明: " + t.Detail);
                sb.AppendLine("    来源: " + t.Source);
                sb.AppendLine();
            }

            string reportPath = Path.Combine(dir, "scan-report.txt");
            try
            {
                File.WriteAllText(reportPath, sb.ToString(), new UTF8Encoding(true));
            }
            catch { }
        }

        private static void WriteCrashLog(Exception ex)
        {
            if (ex == null) return;
            try
            {
                string dir = ResolveDataDir();
                string file = Path.Combine(dir, "crash.log");
                File.AppendAllText(file,
                    "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " + ex.ToString()
                    + Environment.NewLine + Environment.NewLine);
            }
            catch { }
        }
    }
}

三、程序连接

https://download.csdn.net/download/u013083044/93707104

相关推荐
投研帮9 小时前
C#文档管理系统开发实战:技术选型、全文检索与并发控制
c#·.net·文档管理系统
2395730491 天前
在 Avalonia 12 上用纯 .NET 重写 Monaco 编辑器:TKCode.Arc CodeEditor / DiffEditor / LSP 深度架构与实战
c#·.net·lsp·avalonia·monaco·codeeditor·tkcode.arc
新*1 天前
在 .NET 里嵌入 IronPython:内存只涨不降的真相与正确的封装姿势
c#·.net·内存优化·ironpython·dlr
2501_930707781 天前
使用C#从 PowerPoint 演示文稿中提取图片
开发语言·c#·powerpoint
zhanghaha13141 天前
C# 系列教程_15 类 Class 保姆级系统教程(面向新手,大量示例 + 底层机理)
java·开发语言·c#
Risehuxyc2 天前
HttpClient怎么传POST参数
c#
zdr尽职尽责6 天前
UnityCli 兼容2022版本
ui·unity·ai·c#·游戏引擎
BruceNeter7 天前
C# winform卡死问题分析1——线程池子线程创建句柄
c#·.net
何以解忧唯有撸码9 天前
开源】当桌面图标开始打台球:我用 WinForms 做了一桌“真的”台球
c#·winform·自定义控件·gdi
国家一级摸鱼选手9 天前
[Unity]几何碰撞检测:SAT 分离轴定理详解(从数学原理到 C# 实现)
unity·c#·游戏引擎·分离轴定理(sat)·多边形相交