C# 日常使用整理
输入千分位格式数字 string.Format("数字格式 {0:n2},", 123456789)
这个主要和 IFomatProvider 接口有关,NumberFormatInfo numberFomatProvider = new NumberFormatInfo();
C# 中各种流的操作【参考别人博客】
https://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html\
获取日期是多少周
CultureInfo myCI = new CultureInfo("en-US"); System.Globalization.Calendar calendar = myCI.Calendar; int weeks = calendar.GetWeekOfYear(date, CalendarWeekRule.FirstDay,DayOfWeek.Monday);
定时执行
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); //设置30秒处理一次 System.Timers.Timer myTimer = new System.Timers.Timer(1000*30);//修改时间间隔 myTimer.Elapsed += new System.Timers.ElapsedEventHandler(AutoExec); myTimer.AutoReset = true; myTimer.Enabled = true; } private void AutoExec(object sender, System.Timers.ElapsedEventArgs e) { //插入访问日志记录 Logtxt log = new Logtxt(AppDomain.CurrentDomain.BaseDirectory + @"/log/APILog/Log.txt"); log.log("**NowTime**:"+Guid.NewGuid().ToString()); } }
发送邮件
//from email,to email,主题,邮件内容 MailMessage mailmessage = new MailMessage("test@centos.com", "886688668@qq.com", "this is a test", "yes!test!"); mailmessage.Priority = MailPriority.Normal; //邮件优先级 SmtpClient smtpClient = new SmtpClient("mail.centos.com", 25); //smtp地址以及端口号 smtpClient.Credentials = new NetworkCredential("test@centos.com", "*******");//smtp用户名密码 smtpClient.EnableSsl = false; //启用ssl smtpClient.Send(mailmessage); //发送邮件 return Content("发送成功");
Unix时间戳
DateTimeOffset BaseTime = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); long unixTime = (long)(DateTime.Now.ToUniversalTime() - BaseTime).TotalSeconds; long unixTime3 = (long)(DateTime.Now - BaseTime).TotalSeconds; DateTime dt = DateTimeOffset.FromUnixTimeSeconds(unixTime).DateTime; return Content("unixTime:" + unixTime.ToString()+ "--unixTime2:"+ unixTime3.ToString()+"*DateTime:"+ dt.ToString("yyyy-MM-dd HH:mm"));
查看本机Net版本 dotnet --info
查看本机支持的C#版本
csc -langversion:?
dapper 同时读取多个表
string sql = @" SELECT T1.[ProjectItemID] ,T1.[CPU] ,T1.[Memory] ,T1.[ECSLocation] ,T1.[SysdiskType] ,T1.[SysdiskVolume] ,T1.[CreateTime] ,T1.[ECSCount] ,T1.[NeedInternetIP] ,T1.[BandWidth] ,T1.[CreateBy] ,T1.[NeedBackUp] ,T2.TotalPrice ,T2.PricingPeriodID ,T1.OSID,(SELECT [Unit] FROM [Item] WHERE [ID]=T1.OSID) OSName FROM [ECSDetail] T1 INNER JOIN [ProjectItem] T2 ON T1.[ProjectItemID]=T2.[ID] WHERE T1.[ProjectItemID]=@ecsID SELECT [ID] ,[DiskType] ,[DiskVolume] ,[Sort] ,[ECSID] FROM [ECSDisk] WHERE [ECSID]=@ecsID ORDER BY [Sort] "; var multi = connection.QueryMultiple(sql, new { ecsID }); ECSDetail list = multi.Read<ECSDetail>().Single(); list.sasDisk = multi.Read<SASDiskInfo>().ToList();
dapper事务提交
打印到控制台
System.Diagnostics.Debug.WriteLine(“测试”) 会打印到VS输出窗口
监测执行时间
long time = 0; Stopwatch watch = new Stopwatch watch.Stop(); time = watch.ElapsedMilliseconds / 1000;
走代理范问的时候获取真实IP
IP4Address = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
读取环境变量
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");