180it.com 学无止境 - C#
type: channel
link: https://www.180it.com/category/C/
description: C#
items: c# 将字符串 #include <Button2.h> 转义成 html代码格式
items: c# 获取文件夹下 遍历 所有文本名 和内容 并输出
items: c# 提交json 到指定url
items: C#正则提取邮箱,邮箱正则表达式获取邮箱类型
items: QQwry.dat纯真IP数据库读取,C#读取
items: C#从网址中提取域名地址和获取域名解析的IP
items: C# 获取Image图片格式和图片后缀
items: C# DateTime与时间戳TimeStamp互转
items: C#根据银行卡号获取银行名称
items: C# HttpClient应用 下载302跳转文件，获取网页html代码,获取http api接口数据

c# 将字符串 #include <Button2.h> 转义成 html代码格式
type: item
date: 2025-03-22T11:14:47+08:00
description: 封装函数来处理字符串的 HTML 转义。以下是一个示例代码：using System; using System.Net; class Program { static void Main() { string originalString = "#include <Button2.h>"; string encodedString = EncodeToHtml(originalString); Console.WriteLine(encodedString); } static string EncodeToHtml(string input) { return WebUtility.HtmlEncode(input); } }在这个示例中，EncodeToHtml 函数封装了 HTML 转义的逻辑，你可以在需要的时候调用这个函数来处理字符串。

c# 获取文件夹下 遍历 所有文本名 和内容 并输出
type: item
date: 2025-03-22T11:07:44+08:00
description: 在 C# 中，你可以使用 System.IO 命名空间中的类来遍历文件夹下的所有文本文件，并输出它们的文件名和内容。以下是一个示例代码，展示了如何实现这一功能：using System; using System.IO; class Program { static void Main(string[] args) { string folderPath = @"C:\path\to\your\folder"; // 替换为你的文件夹路径 // 获取文件夹下所有文本文件 string[] files = Directory.GetFiles(folderPath, "*.txt", SearchOption.AllDirectories); foreach (string file in files) { // 输出文件名 Console.WriteLine($"文件名: {Path.GetFileName(file)}"); // 读取并输出文件内容 string content = File.ReadAllText(file); Console.WriteLine("内容:"); Console.WriteLine(content); Console.WriteLine(new string('-', 50)); // 分隔符 } } } 在这个示例中：使用 Directory.GetFiles 方法获取指定文件夹及其子文件夹下的所有 .txt 文件。使用 Path.GetFileName 方法获取文件名。使用 File.ReadAllText 方法读取文件内容。输出文件名和内容。请将 folderPath 替换为你要遍历的文件夹路径。运行此程序后，它将输出指定文件夹下所有文本文件的文件名和内容。

c# 提交json 到指定url
type: item
date: 2025-03-22T11:06:31+08:00
description: using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; class Program { static async Task Main(string[] args) { var url = "https://www.xxxx.com/info/info-add-ok_soft.php"; var data = new { title2 = "arduino", classid = "3", title = "标题", keywords = "关键词", description = "备注", text = " xxx 内容 ", state = "1" }; var json = JsonConvert.SerializeObject(data); var content = new StringContent(json, Encoding.UTF8, "application/json"); using (var client = new HttpClient()) { var response = await client.PostAsync(url, content); var responseString = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseString); } } }

C#正则提取邮箱,邮箱正则表达式获取邮箱类型
type: item
date: 2024-11-19T11:17:55+08:00
description: static void Main(string[] args) { string str = File.ReadAllText("1.txt", Encoding.Default); MatchCollection mc = Regex.Matches(str, @"([-a-zA-Z_0-9.]+)@([-a-zA-Z0-9_]+(\.[a-zA-Z]+)+)"); foreach (Match m in mc) { Console.WriteLine(m.Value); Console.WriteLine(m.Groups[2].Value);//([-a-zA-Z_0-9.]+)@([-a-zA-Z0-9_]+(\.[a-zA-Z]+)+)")这是第一组，([-a-zA-Z_0-9.]+)第二组([-a-zA-Z0-9_]+(\.[a-zA-Z]+)+)")第三组(\.[a-zA-Z]+)+)第四组 Console.WriteLine("========="); } Console.WriteLine(mc.Count); Console.ReadKey(); }

QQwry.dat纯真IP数据库读取,C#读取
type: item
date: 2024-11-19T11:17:09+08:00
description: 以下是读取QQwry.Dat数据库类库using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace QQwry.dat纯真IP数据库读取 { public class IPSearch { FileStream ipFile; long ip; string ipfilePath; /// /// 构造函数 /// /// 纯真IP数据库路径 public IPSearch(string ipfilePath) { this.ipfilePath = ipfilePath; } //测试 /// /// 地理位置,包括国家和地区 /// public struct IPLocation { public string country, area; } /// /// 获取指定IP所在地理位置 /// /// 要查询的IP地址 ///<returns></returns> public IPLocation GetIPLocation(string strIP) { ip = IPToLong(strIP); ipFile = new FileStream(ipfilePath, FileMode.Open, FileAccess.Read); long[] ipArray = BlockToArray(ReadIPBlock()); long offset = SearchIP(ipArray, 0, ipArray.Length - 1) * 7 + 4; ipFile.Position += offset;//跳过起始IP ipFile.Position = ReadLongX(3) + 4;//跳过结束IP IPLocation loc = new IPLocation(); int flag = ipFile.ReadByte();//读取标志 if (flag == 1)//表示国家和地区被转向 { ipFile.Position = ReadLongX(3); flag = ipFile.ReadByte();//再读标志 } long countryOffset = ipFile.Position; loc.country = ReadString(flag); if (flag == 2) { ipFile.Position = countryOffset + 3; } flag = ipFile.ReadByte(); loc.area = ReadString(flag); ipFile.Close(); ipFile = null; return loc; } /// /// 将字符串形式的IP转换位long /// /// ///<returns></returns> public long IPToLong(string strIP) { byte[] ip_bytes = new byte[8]; string[] strArr = strIP.Split(new char[] { '.' }); for (int i = 0; i < 4; i++) { ip_bytes[i] = byte.Parse(strArr[3 - i]); } return BitConverter.ToInt64(ip_bytes, 0); } /// /// 将索引区字节块中的起始IP转换成Long数组 /// /// long[] BlockToArray(byte[] ipBlock) { long[] ipArray = new long[ipBlock.Length / 7]; int ipIndex = 0; byte[] temp = new byte[8]; for (int i = 0; i < ipBlock.Length; i += 7) { Array.Copy(ipBlock, i, temp, 0, 4); ipArray[ipIndex] = BitConverter.ToInt64(temp, 0); ipIndex++; } return ipArray; } /// /// 从IP数组中搜索指定IP并返回其索引 /// /// IP数组 /// 指定搜索的起始位置 /// 指定搜索的结束位置 ///<returns></returns> int SearchIP(long[] ipArray, int start, int end) { int middle = (start + end) / 2; if (middle…

C#从网址中提取域名地址和获取域名解析的IP
type: item
date: 2024-11-19T11:16:18+08:00
description: /// /// 通过域名获得域名解析的IP地址 /// /// 网址 /// <returns>返回域名解析的IP地址</returns> private string GetYuMingIP(string url) { string rIP = string.Empty; string p = @"(http|https)://(?<domain>[^(:|/]*)"; Regex reg = new Regex(p, RegexOptions.IgnoreCase); string ipAddress = url; if (!ipAddress.Contains("http")) { ipAddress = "http://" + ipAddress; } Match m = reg.Match(ipAddress); string Result = m.Groups["domain"].Value;//域名地址 如http://wwww.luofenmng.com/index.aspx 提取出来的是www.luofenming.com //以下是获取域名解析的IP地址 try { IPHostEntry host = Dns.GetHostByName(Result); IPAddress ip = host.AddressList[0]; rIP = ip.ToString(); } catch { rIP = "请输入正确的域名,或者您的电脑没有联互联网"; } return rIP; }

C# 获取Image图片格式和图片后缀
type: item
date: 2024-11-19T11:14:35+08:00
description: private ImageFormat GetImageFormat(FileStream file, out string format) { byte[] sb = new byte[2]; //这次读取的就是直接0-1的位置长度了. file.Read(sb, 0, sb.Length); //根据文件头判断 string strFlag = sb[0].ToString() + sb[1].ToString(); //察看格式类型 switch (strFlag) { //JPG格式 case "255216": format = ".jpg"; return ImageFormat.Jpeg; //GIF格式 case "7173": format = ".gif"; return ImageFormat.Gif; //BMP格式 case "6677": format = ".bmp"; return ImageFormat.Bmp; //PNG格式 case "13780": format = ".png"; return ImageFormat.Png; //其他格式 default: format = string.Empty; return null; } } private ImageFormat GetImageFormat(Image _img, out string format) { if (_img.RawFormat.Equals(ImageFormat.Jpeg)) { format = ".jpg"; return ImageFormat.Jpeg; } if (_img.RawFormat.Equals(ImageFormat.Gif)) { format = ".gif"; return ImageFormat.Gif; } if (_img.RawFormat.Equals(ImageFormat.Png)) { format = ".png"; return ImageFormat.Png; } if (_img.RawFormat.Equals(ImageFormat.Bmp)) { format = ".bmp"; return ImageFormat.Bmp; } format = string.Empty; return null; } public string GetMimeType(Image image) { ImageCodecInfo[] imageCodec = ImageCodecInfo.GetImageDecoders(); foreach (ImageCodecInfo item in imageCodec) { if (item.FormatID == image.RawFormat.Guid) { // 返回 样式为 image/png，image/gif 等样式字符串 return item.MimeType; } } return "unknown"; }

C# DateTime与时间戳TimeStamp互转
type: item
date: 2024-11-19T11:14:02+08:00
description: /// /// DateTime转时间戳 /// /// DateTime /// <returns>时间戳（秒）</returns> public static long GetUnixTimeStamp(DateTime dt) { DateTime dtStart = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1, 0, 0, 0), TimeZoneInfo.Local); long timeStamp = Convert.ToInt32((dt - dtStart).TotalSeconds); return timeStamp; } /// /// 时间戳转DateTime /// /// 时间戳（秒） /// <returns>DateTime</returns> public static DateTime GetDateTimeFromUnixTimeStamp(long timeStamp) { DateTime dtStart = TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1, 0, 0, 0), TimeZoneInfo.Local); long lTime = ((long)timeStamp * 10000000); TimeSpan toNow = new TimeSpan(lTime); DateTime targetDt = dtStart.Add(toNow); return targetDt; }

C#根据银行卡号获取银行名称
type: item
date: 2024-11-19T11:13:37+08:00
description: C#根据银行卡号获取银行名称核心代码/// /// 银行信息 /// public class BankInfo { #region 数组形式存储银行BIN号 /// /// 银行 BIN 号 /// private readonly static long[] BankBin = { 102033, 103000, 185720, 303781, 356827, 356828, 356833, 356835, 356837, 356838, 356839, 356840, 356885, 356886, 356887, 356888, 356889, 356890, 370246, 370247, 370248, 370249, 400360, 400937, 400938, 400939, 400940, 400941, 400942, 402658, 402673, 402791, 403361, 403391, 404117, 404157, 404171, 404172, 404173, 404174, 404738, 404739, 405512, 405512, 406252, 406254, 406365, 407405, 409665, 409666, 409667, 409668, 409669, 409670, 409671, 409672, 410062, 412962, 412963, 415599, 421317, 421349, 421393, 421437, 421865, 421869, 421870, 421871, 422160, 422161, 424106, 424107, 424108, 424109, 424110, 424111, 424902, 425862, 427010, 427018, 427019, 427020, 427028, 427029, 427038, 427039, 427062, 427064, 427571, 428911, 431502, 431502, 433666, 433670, 433680, 434061, 434062, 435744, 435745, 436718, 436728, 436738, 436742, 436745, 436748, 436768, 438088, 438125, 438126, 438588, 438589, 438600, 439188, 439225, 439227, 442729, 442730, 451289, 451291, 451804, 451804, 451810, 451810, 453242, 456351, 456418, 458060, 458060, 458071, 458071, 458123, 458124, 468203, 472067, 472068, 479228, 479229, 481699, 486466, 486493, 486494, 486497, 487013, 489592, 489734, 489735, 489736, 491020, 491020, 491031, 491032, 491040, 493427, 493878, 498451, 504923, 510529, 512315, 512316, 512411, 512412, 512425, 512431, 512466, 512695, 512732, 514906, 514957, 514958, 517636, 518212, 518364, 518378, 518379, 518474, 518475, 518476, 518710, 518718, 519412, 519498, 520082, 520108, 520131, 520152, 520169, 520194, 520382, 521899, 522153, 523036, 524011, 524047, 524070, 524091, 524094, 524864, 524865, 525498, 525745, 525746, 526410, 526855, 527414, 528020, 528931, 528948, 530970, 530980, 530980, 530990, 532420, 532430, 532450, 532458, 535910, 535910, 535918, 537830, 540297, 540838, 541068, 541709, 543159, 544033, 545619, 545623, 545947, 547628, 547648, 547766, 547766, …

C# HttpClient应用 下载302跳转文件，获取网页html代码,获取http api接口数据
type: item
date: 2024-11-19T11:12:27+08:00
description: /// /// 下载 302跳转文件 /// /// 父Url /// 302跳转的Url /// <returns></returns> public bool DownFile(string parentUrl,string url) { using (HttpClient client = new HttpClient()) { Uri uri = new Uri(parentUrl); client.DefaultRequestHeaders.Referrer = uri; var responseMessage = client.GetAsync(url).Result; if (responseMessage.IsSuccessStatusCode) { //var v = responseMessage.Content.Headers.ContentType;//内容类型 using (var fs = File.Create("d:\\1.pdf")) { // 获取结果，并转成 stream 保存到本地。 var streamFromService = responseMessage.Content.ReadAsStreamAsync().Result; streamFromService.CopyTo(fs); responseMessage.Dispose(); streamFromService.Dispose(); return true; } } } return false; } /// /// 下载 文件 /// /// 父Url /// 302跟转的Url /// <returns></returns> public bool DownFile(string url) { using (HttpClient client = new HttpClient()) { var responseMessage = client.GetAsync(url).Result; if (responseMessage.IsSuccessStatusCode) { //var v = responseMessage.Content.Headers.ContentType;//内容类型 using (var fs = File.Create("d:\\1.pdf")) { // 获取结果，并转成 stream 保存到本地。 var streamFromService = responseMessage.Content.ReadAsStreamAsync().Result; streamFromService.CopyTo(fs); responseMessage.Dispose(); streamFromService.Dispose(); return true; } } } return false; } public string Get(string urlStr) { using (HttpClient client = new HttpClient()) { var responseMessage = client.GetAsync(urlStr).Result; if (responseMessage.IsSuccessStatusCode) { string data = responseMessage.Content.ReadAsStringAsync().Result; return data; } else { return string.Empty; } } } public string Post(string urlStr,string data) { using (HttpClient client = new HttpClient()) { HttpContent content = new StringContent(data); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); string result = client.PostAsync(urlStr, content).Result.Content.ReadAsStringAsync().Result; return result; } } /// /// httpclient中携带cookie /// /// /// cookie字符串 /// <returns></returns> public string Get(string url, string cookie…