Mat*_*hew 0 c# console clipboard
我在C#中有以下控制台应用程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Forms;
namespace PreventScreenshot
{
class Program
{
[STAThread]
public static void Main(string[] args)
{
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new ElapsedEventHandler(timer_Tick);
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
Console.WriteLine("---Prevent Screenshot from being taken---");
Console.WriteLine();
Console.WriteLine("DLL operation started. Try to take a screenshot");
Console.WriteLine();
Console.WriteLine("Press enter to exit");
Console.ReadLine();
}
public static void timer_Tick(object sender, EventArgs e)
{
Clipboard.Clear();
}
}
}
Run Code Online (Sandbox Code Playgroud)
据推测,应用程序每秒都会清除剪贴板.但是,它不起作用.问题是什么?
编辑:
我刚刚编辑了代码.当我尝试运行程序时,剪贴板仍然没有清除.怎么了?
Console.ReadLine()之后移动timer1.Start()
目前,您的应用程序正在等待输入按下,然后启动计时器并立即存在.
关于你编辑的帖子:
您使用的Timer是System.Windows.Forms,它不适合控制台应用程序.尝试使用计时器System.Timers.