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);