C#找出可用TCP端口,仅两行代码就搞定

var start = 2222;
var ps = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners().Select(e => e.Port).Where(e => e > start).OrderBy(e => e).ToList();
var port = ps.Take(ps.Count - 1).Where((e, i) => ps[i + 1] - e > 1).FirstOrDefault();

 

posted @ 2019-11-25 15:53  大胡子毛绒老头  阅读(309)  评论(0编辑  收藏  举报