C#-常用知识点
1.日期相关
获取英文月份名称 : DateTime.Now.ToString("MMMM")
1.1 各个字母所代表的意思
1.MM:月份 2.mm:分钟 3. MMMM:文字形式月份 4.MMM:三个字母缩写的月份
4.HH:24小时制 5.hh:12小时制
6.ddd:三个字母缩写的星期 7.dddd:完整的星期
8.t: 单字母 A.M./P.M. 缩写(A.M. 将显示为“A”) 9.tt:两字母 A.M./P.M. 缩写(A.M. 将显示为“AM”)
9.zz 时区(eg:+8)
10.d (eg:11/3/2017)
2. 拖拽图片显示到 PictureBox中
首先 修改窗体的 AllowDrop 属性为true ,然后在 DragEnter 事件中写下如下代码,实际上就是获取存放拖放文件的本地路径
private void FrmAddFood_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;//设置拖放操作中目标放置类型为复制
string[] str_Drop = (string[])e.Data.GetData(DataFormats.FileDrop, true);//拖放的多个文件的路径列表
string tempstr = str_Drop[0];//获取拖放第一个文件的路径
try
{
Image img = Image.FromFile(tempstr);//存储拖放的图片
pic.Image = img;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
3.string数组转换为int数组
string[] strs = { "1", "2", "3" };
int[] nums = Array.ConvertAll<string, int>(strs, t => int.Parse(t));
4.获取屏幕尺寸
//不包含任务栏
int width1 = SystemInformation.WorkingArea.Width;
int height1 = SystemInformation.WorkingArea.Height;
//包含任务栏
int width2 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
int height2 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
5.压缩图片,生成缩略图
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="sourceImage">源图</param>
/// <param name="width">新图宽</param>
/// <param name="height">新图高</param>
/// <returns>新图</returns>
public Image GetReducedImage(Image sourceImage, int width, int height)
{
Bitmap bitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bitmap);
g.Clear(Color.Transparent); //清空背景图,以透明色填充
//在新图片的指定位置绘制指定大小的原图片对象
g.DrawImage(sourceImage, new Rectangle(0, 0, width, height));
return bitmap;
}
方式二:
img.GetThumbnailImage(width, height, null, IntPtr.Zero);
6.货币格式字符串转换为 Int
int str= int.Parse("$123,123.00", System.Globalization.NumberStyles.AllowThousands| System.Globalization.NumberStyles.AllowDecimalPoint| ystem.Globalization.NumberStyles.AllowCurrencySymbol);
7.修改配置文件
private void SetAppSetting(string key, string value)
{
// 1.Debug目录下的配置文件
Configuration config = ConfigurationManager.OpenExeConfiguration("UI.exe");
config.AppSettings.Settings[key].Value = value;
config.Save();
// 2.项目目录下的配置文件
//获取配置文件目录
string path = Environment.CurrentDirectory;
path = path.Substring(0, path.Length - 9) + "App.config";
//修改指定属性值
XDocument doc = XDocument.Load(path);
var rts = doc.Root.Element("appSettings");
rts.Elements().Single(t => t.Attribute("key").Value == key).Attribute("value").Value = value;
doc.Save(path);
}
8.获取当前程序运行绝对路径
Environment.CurrentDirectory
9.无边框窗体拖动实现
int x;
int y;
private void Item_MouseDown(object sender, MouseEventArgs e)
{
x = e.X;
y = e.Y;
}
private void Item_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + (e.X - this.x), this.Location.Y + (e.Y - this.y));
}
}
10.转换Linq结果集为 DataTable
public static DataTable ToDataTable<T>(IEnumerable<T> ds)
{
DataTable dt = new DataTable();
//Column Name
foreach (var item in ds.First().GetType().GetProperties())
{
dt.Columns.Add(item.Name);
}
//Datarow
foreach (var item in ds)
{
DataRow dr = dt.NewRow();
foreach (DataColumn col in dt.Columns)
{
dr[col.ColumnName] = item.GetType().GetProperty(col.ColumnName).GetValue(item);
}
dt.Rows.Add(dr);
}
return dt;
}
11.生成唯一字符串
Guid.NewGuid().ToString("N");
12. C#实现中国式四舍五入
Math.Round(123.125, 2, MidpointRounding.AwayFromZero);
C#-常用知识点的更多相关文章
- DB2_SQL_常用知识点&实践
DB2_SQL_常用知识点&实践 一.删除表中的数据(delete或truncate) 1 truncate table T_USER immediate; 说明:Truncate是一个能够快 ...
- JAVA常用知识点及面试题总结
1. String.StringBuffer.StringBuilder三者区别? (1)三者在执行速率上的比较: String<StringBuffer<StringBuilder 原因 ...
- HTML常用知识点代码演示
1 HTML部分常用知识点 <!-- 版本声明 --> <!DOCTYPE html> <!-- 唯一根元素 --> <html> <!-- 对网 ...
- Java 常用知识点
Java 常用知识点 1.日期格式化 SimpleDateFormat Date date=new Date(System.currentTimeMillis()) ; SimpleDateForma ...
- Less常用知识点
上篇文章介绍了如何安装Less,我们将所有东西都写在.less里面,最后通过命令将.less转换成.css文件,就可以放入到项目里用了.今天了解一些less常用知识点. 1.变量:声明两个变量,一个是 ...
- BIOS备忘录之EC常用知识点
BIOS工程师眼中常用的EC知识点汇总: EC的硬件架构 EC硬件结构上主要分为两部分:Host Domain和EC Domain Host Domain就是通过LPC与CPU通信的部分(LPC部分需 ...
- YII2常用知识点总结
YII2常用知识点总结 (一)总结性语句 (1)经常看看yii源码比如vendor\yiisoft\yii2\web这个目录(很重要)下的文件中的方法(这些文件中的公共方法,大致看了下基本上都可以通过 ...
- CSS3常用知识点
CSS3常用知识点 1 css3选择器 1.1 属性选择器 /* E[attr~=val] 表示的一个单独的属性值 这个属性值是以空格分隔的*/ .attr2 a[class~="kawa& ...
- javaScript常用知识点有哪些
javaScript常用知识点有哪些 一.总结 一句话总结:int = ~~myVar, // to integer | 是二进制或, x|0 永远等于x:^为异或,同0异1,所以 x^0 还是永远等 ...
- 一文学会 TypeScript 的 82% 常用知识点(下)
一文学会 TypeScript 的 82% 常用知识点(下) 前端专栏 2019-11-23 18:39:08 都已经 9021 年了,TypeScript(以下简称 TS)作为前端工程师不得 ...
随机推荐
- android studio 包名冲突解决
Error: Execution failed for task ': app: packageAllDebugClassesForMultiDex'. > Java.util.zip.ZipE ...
- 【python】json中字典key不可为数值型
遇到了一个很诡异的错误.写一个字典,存入json文件.之后读出判断是否存在key.结果惊奇的发现,同一个key居然存在两次. 原因:json会将数值key转换为unicode 结论:使用json时字典 ...
- 启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space
三种因素引起: 1.jvm(jdk)的内存引起. 2. eclipse的内存引起. 3.tomcat的内存引起. 1.解决方法: 2.解决方法: 解决问题的方式就是:修改了安装目录eclipse.in ...
- Python函数之递归函数
递归函数的定义:在这个函数里再调用这个函数本身 最大递归深度默认是997或者998,python从内存角度做的限制 优点:代码变简单 缺点:占内存 一:推导年龄 问a的值是多少: a 比 b 小2,b ...
- [转] 安装npm全局包提示权限不够
方法1 sudo npm i -g npm 方法2 修改usr/local的权限.使用sudo有一个风险是安装包可能会运行自己的一些脚本,使sudo操作变的不可控,不安全.可以通过将/usr/loca ...
- 期货大赛项目|二,DAL详解
接口层就不重点讲述了,直接DAL层 DAL层 using System; using System.Collections.Generic; using System.Linq; using Syst ...
- mysql的下载与安装
官网 下载 https://www.mysql.com/ 依次点击:downloads>community>mysql community server,如图所示 image.png ...
- IDEA上创建 Maven SpringBoot+mybatisplus+thymeleaf 项目
概述 在WEB领域,Java也是在不断的探索和改进,从开始的JSP--->Struts1--->Struts2+Spring--->Spring MVC--->SpringBo ...
- 优化 Markdown 在 Notepad++ 中的使用体验
选择一个强大而好用的文本编辑器,是进行 Web 开发和编程必不可少的一部分,甚至对于通常的写作,一个舒服的文本编辑器也会让你写起文字来觉得优雅而潇洒.Sublime Text 是一款不错的编辑器,简洁 ...
- Derive representation formula from Green’s identity
This article introduces how to derive the representation formula used in BEM from Green's identity. ...