我们知道在 dotnet 基础框架体系中,是没有提供函数可直接访问操作系统命令的,所以一般来说我们要实现类似的功能都需要创建一个新的匿名管道的后台 cmd 进程(配置;stdout、stdin、stderr 通道)但显然这种方式本身就具有很多的局限性,性能的消耗只是其中一点而已,但我们可以直接调用系统提供的API,则可避免这些问题的,显然前面提到“dotnet”基础框架体系是没有提供这个接口的,所以我们需要手动实现且去调用它,好在就实现的角度来说是很 simple 的。
[DllImport("msvcrt.dll", SetLastError = false, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private extern static void system(string command); // longjmpstatic void Main(string[] args)
{
system("title test");
system("arp -a");
Console.ReadKey(false);
}