禁用control+c关闭程序
我有一个循环 ping 请求的方法,我希望当我单击 Ctrl+c 时,它会打破循环并为我提供像普通 cmd 一样的静态信息,但是当我点击 Ctrl+c,我得到
按任意键继续... <<
然后程序关闭。
例如: ping google.com -t <<这将通过无限循环 ping google,但仅当我单击 Ctrl+c 时才需要打破循环
private void _t(string website)
{
Ping pingSender = new Ping();
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 10000;
try
{
PingOptions options = new PingOptions(64, false);
PingReply send0 = pingSender.Send(website, timeout, buffer, options);
Console.WriteLine("\nPinging {0} [{1}] With {2} bytes of data :", website, send0.Address.ToString(), send0.Buffer.Length);
while (1 < 2)
{
PingReply reply = pingSender.Send(website, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime / 5, reply.Options.Ttl);
}
else
{
Console.WriteLine("Request timed out.");
}
}
}
catch
{
Console.WriteLine("Ping request could not find host {0} ", website + ".Please check the name and try again.");
}
}
,而不是使用 while (1<2)< /code>,我想使用类似的东西:
while (ConsoleKeyInfo.Equals("Control + c") not clicked) // sure it is wrong , but that`s what I wanna achieve
{
PingReply reply = pingSender.Send(website, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime / 5, reply.Options.Ttl);
}
else
{
Console.WriteLine("Request timed out.");
}
}
I have a method that loops ping requests, and I'd like when I click Ctrl+c, it break the loop and give me statics like the normal cmd, but when I click Ctrl+c, I get
Press Any Key To Continue... <<
and then program closes .
For example : ping google.com -t << that will ping google with endless loop, but I need to break the loop ONLY when I click Ctrl+c
private void _t(string website)
{
Ping pingSender = new Ping();
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 10000;
try
{
PingOptions options = new PingOptions(64, false);
PingReply send0 = pingSender.Send(website, timeout, buffer, options);
Console.WriteLine("\nPinging {0} [{1}] With {2} bytes of data :", website, send0.Address.ToString(), send0.Buffer.Length);
while (1 < 2)
{
PingReply reply = pingSender.Send(website, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime / 5, reply.Options.Ttl);
}
else
{
Console.WriteLine("Request timed out.");
}
}
}
catch
{
Console.WriteLine("Ping request could not find host {0} ", website + ".Please check the name and try again.");
}
}
But instead of using while (1<2)
, I want to use something like :
while (ConsoleKeyInfo.Equals("Control + c") not clicked) // sure it is wrong , but that`s what I wanna achieve
{
PingReply reply = pingSender.Send(website, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Reply from {0}: Bytes {1} time={2} TTL={3}", reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime / 5, reply.Options.Ttl);
}
else
{
Console.WriteLine("Request timed out.");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能有点晚了,但这是一种更好的方法,适用于所有平台:
I might be a little late, but here's a better way, one that works on all platforms:
有一个相当完整的教程(带有代码)此处。我做了一个快速测试,他们的例子确实有效(想象一下)。
这是他们的代码:
There is a fairly complete tutorial (with code) here. I did a quick test and their example actually worked (imagine that).
Here is their code:
另一种方法是处理 Console.CancelKeyPress 事件:
这允许您保留快捷方式,但根据具体情况决定是否退出。
Another way is to handle the Console.CancelKeyPress event:
This allows you to keep the shortcut, but decide whether to exit on a case by case basis.