[.Net之C#常用实例]开发笔记一
1.计算当前时间和目标时间的时间间隔
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { public class Program { static void Main(string[] args) { DateTime now = DateTime.Now; DateTime tar = new DateTime(2024, 11, 19, 12, 0, 0); TimeSpan ts = tar.Subtract(now); Console.WriteLine($"当前时间:{now.ToString()}"); Console.WriteLine($"目标时间:{tar.ToString()}"); Console.WriteLine($"获取以整分钟数和分钟的小数部分表示:{ts.TotalMinutes}"); Console.WriteLine($"获取以整秒数和秒的小数部分表示:{ts.TotalSeconds}"); Console.ReadKey(true); } } }

2.获取屏幕尺寸
this.width = Screen.PrimaryScreen.Bounds.Width;//屏幕宽度 this.height = Screen.PrimaryScreen.Bounds.Height;//屏幕高度
3.将两个字符串组合成一个路径
string path = @"e:\abcd"; string filename = Path.Combine(path,"zx.txt");
输出:e:\abcd\zx.txt
4.解析域名对应的ipv6或ipv4地址
IPAddress ipv4 = Dns.GetHostAddresses("cnblogs.cn")[0]; IPAddress ipv6 = Dns.GetHostAddresses("cnblogs.cn")[1];
5.获取运行应用程序的存储路径
Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);

浙公网安备 33010602011771号