C#实现端口扫描和执行cmd命令、调用摄像头

C#端口扫描

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace PortScanner
{
    class Program
    {
        static void Main(string[] args)
        {
            // 设置扫描参数
            string host = "localhost";
            int startPort = 1;
            int endPort = 65535;
            int numThreads = 1000;

            // 创建线程池并开始扫描
            ThreadPool.SetMinThreads(numThreads, numThreads);
            List<WaitHandle> handles = new List<WaitHandle>();
            for (int port = startPort; port <= endPort; port++)
            {
                handles.Add(new ManualResetEvent(false));
                ThreadPool.QueueUserWorkItem(CheckPort, new object[] { host, port, handles.Last() });
            }

            // 等待所有线程完成
            WaitHandle.WaitAll(handles.ToArray());
            Console.WriteLine("Scanning complete.");
        }

        static void CheckPort(object args)
        {
            // 解析参数
            object[] parameters = (object[])args;
            string host = (string)parameters[0];
            int port = (int)parameters[1];
            ManualResetEvent handle = (ManualResetEvent)parameters[2];

            // 尝试连接端口
            try
            {
                using (TcpClient client = new TcpClient())
                {
                    client.Connect(host, port);
                    Console.WriteLine("Port {0} is open.", port);
                }
            }
            catch (Exception)
            {
                // 端口未打开
            }

            // 通知主线程完成
            handle.Set();
        }
    }
}

使用方法:

  1. 打开 Visual Studio 或类似的 IDE
  2. 新建一个 C# 控制台应用程序
  3. 将上述代码复制粘贴到 Program.cs 文件中
  4. 调整 hoststartPortendPortnumThreads 等参数
  5. 在菜单中选择“生成”,然后选择“生成解决方案”
  6. 在菜单中选择“调试”,然后选择“调试 C# 程序”
  7. 程序将开始扫描指定主机的指定端口,输出开放的端口号

使用多线程可以提高 C# 端口扫描的速度。在上述代码中,我们使用了线程池来创建多个线程来扫描指定主机的端口。可以通过调整 numThreads 参数来控制线程池的大小。

同时,如果使用异步 I/O 操作来连接端口,也可以进一步提高扫描速度。可以使用 Socket.BeginConnect() 方法来启动异步连接,并使用 Socket.EndConnect() 方法来等待连接完成。

需要注意的是,使用多线程和异步 I/O 操作都会增加程序的复杂度。如果不正确地使用这些技术,可能会导致程序出现各种问题,例如竞态条件和死锁。因此,在使用这些技术的时候,请务必小心并仔细测试程序。

这个错误是由于同时创建的 WaitHandle 实例数超过了 64 个所导致的。

解决方法是使用更大的线程池,或将线程分批执行。例如,可以将端口分成几个范围,每个范围分配一个线程池,然后依次扫描每个范围。或者可以使用异步 I/O 操作来连接端口,这样可以避免创建过多的线程。

另外,需要注意的是,使用多线程和异步 I/O 操作都会增加程序的复杂度。如果不正确地使用这些技术,可能会导致程序出现各种问题,例如竞态条件和死锁。因此,在使用这些技术的时候,请务必小心并仔细测试程序。

using System;
using System.Diagnostics;

namespace CmdExecution
{
    class Program
    {
        static void Main(string[] args)
        {
            // 执行命令并输出结果
            string command = "dir";
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c " + command;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            Console.WriteLine(process.StandardOutput.ReadToEnd());
            process.WaitForExit();
        }
    }
}

上述代码使用 C# 执行 cmd 命令,并将结果输出到控制台中。使用 Process.Start() 方法启动 cmd.exe 进程,并使用 /c 参数执行指定的命令。使用 Process.StandardOutput 属性获取命令输出,并使用 Console.WriteLine() 方法将输出写入控制台。

需要注意的是,使用此方法可以执行任意的 cmd 命令,包括危险的命令。因此,在实际应用中,需要对输入进行严格的验证和过滤,以确保系统安全。同时,也需要注意系统权限的限制,某些命令可能需要管理员权限才能执行。

以下是使用 C# 判断是否存在摄像头并打开摄像头的示例代码:

using System;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CameraExample
{
    class Program
    {
        [DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")]
        private static extern IntPtr capCreateCaptureWindow(string lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hwndParent, int nID);

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        static void Main(string[] args)
        {
            // 检查是否存在摄像头
            bool hasCamera = false;
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE Caption like '%Camera%'");
            foreach (ManagementObject device in searcher.Get())
            {
                hasCamera = true;
                break;
            }

            // 如果存在摄像头,则打开摄像头窗口
            if (hasCamera)
            {
                IntPtr hwnd = capCreateCaptureWindow("Camera", 0, 0, 0, 640, 480, IntPtr.Zero, 0);
                SetForegroundWindow(hwnd);
                Application.Run();
            }
            else
            {
                Console.WriteLine("No camera found.");
            }
        }
    }
}

上述代码使用 WMI 查询来判断是否存在摄像头。如果存在摄像头,则使用 capCreateCaptureWindow() 函数创建摄像头窗口,并使用 SetForegroundWindow() 函数将窗口置于前台。最后,使用 Application.Run() 函数启动消息循环,以便用户可以操作摄像头窗口。如果不存在摄像头,则输出错误信息。

需要注意的是,使用此方法只能打开第一个摄像头并显示默认分辨率的图像。如果需要更多的摄像头支持和更高的图像质量,请使用专业的摄像头库或框架。同时,也需要注意系统权限的限制,某些操作可能需要管理员权限才能执行。

猜你喜欢

转载自blog.csdn.net/Documentlv/article/details/132098220