自定义线帽
/// <summary>
/// 画笔
/// </summary>
private Pen pen = new Pen(Color.Black);
/// <summary>
/// 自定义线帽
/// </summary>
private AdjustableArrowCap lineCap = new AdjustableArrowCap(10, 10, false);
pen.CustomEndCap = lineCap;
e.Graphics.DrawLine(pen, new Point(24,310), new Point(720, 310));
ListView/GridView一句话开启双缓冲
this.listView1.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(this.listView1, true, null);
杀死进程
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
public static void Kill(MSExcel.Application excel)
{
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
Dictionary使用linq排序
Dictionary<int, double> percents = new Dictionary<int, double>();
//排序
var dicSort = (from d in percents orderby d.Value select d);
foreach (var item in dicSort)
{
System.Console.WriteLine(item.Value);
}
获取Panel的内容到Bitmap
Bitmap bmp = new Bitmap(this.panelHistControl1.panel1.Width, this.panelHistControl1.panel1.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
this.panelHistControl1.panel1.DrawToBitmap(bmp, new Rectangle(0, 0, this.panelHistControl1.panel1.Width, this.panelHistControl1.panel1.Height));
文件关联,双击打开应用程序
下面是写注册表,写入完注册表之后,双击就会打开你的应用,并且再Main(string[] args) 方法获得双击的文件的完整路径,之后就可以操作文件了
private void WriteApp()
{
string strProject = "YourAppName"; //应用名
string p_FileTypeName = ".selfFileExt"; //自定义文件后缀
string fileName = System.Windows.Forms.Application.ExecutablePath;// 获取启动了应用程序的可执行文件的路径及文件名
//string startPath = System.Windows.Forms.Application.StartupPath;//获取启动了应用程序的可执行文件的路径
//注册文件类型
RegistryKey _RegKey = Registry.ClassesRoot.OpenSubKey("", true); //打开注册表
RegistryKey _VRPkey = _RegKey.OpenSubKey(p_FileTypeName, true);
if (_VRPkey != null)
{
_RegKey.DeleteSubKey(p_FileTypeName, true);
}
_RegKey.CreateSubKey(p_FileTypeName);
_VRPkey = _RegKey.OpenSubKey(p_FileTypeName, true);
_VRPkey.SetValue("", strProject);
_VRPkey = _RegKey.OpenSubKey(strProject, true);
if (_VRPkey != null) _RegKey.DeleteSubKeyTree(strProject); //如果等于空就删除注册表DSKJIVR
Registry.ClassesRoot.CreateSubKey(p_FileTypeName).SetValue("", strProject, RegistryValueKind.String);
using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(strProject))
{
//设置图标
RegistryKey iconKey = key.CreateSubKey("DefaultIcon");
iconKey.SetValue("", System.Windows.Forms.Application.StartupPath + "\\logo.ico");
//设置默认启动程序
key.CreateSubKey(@"Shell\Open\Command").SetValue("", "\"" + fileName + "\" \"%1\"");
}
}
对象序列化/反序列化
#序列化
using (System.IO.FileStream fs = new System.IO.FileStream("你的欲保存的文件路径", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, data);//data是你的可序列化的对象
fs.Close();
}
#反序列化
System.IO.FileStream fs = new System.IO.FileStream("你的文件路径", FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter formatter = new BinaryFormatter();
YouSelfData data = formatter.Deserialize(fs) as YouSelfData;//YouSelfData 你的自定义的类
判断闰年
public static bool IsLeapYear(int year)
{
bool b1 = (year % 4 == 0) && (year % 100 != 0);
bool b2 = (year % 400 == 0);
return b1 || b2;
}
反编译器
ILSpy 是开源的 .NET 程序集浏览器和反编译器。
下载地址:
https://github.com/icsharpcode/ILSpyhttps://github.com/icsharpcode/ILSpy
Database.SetInitializer的几种参数
数据库不存在时重新创建数据库
Database.SetInitializer<testContext>(new CreateDatabaseIfNotExists<testContext>());
每次启动应用程序时创建数据库
Database.SetInitializer<testContext>(new DropCreateDatabaseAlways<testContext>());
模型更改时重新创建数据库
Database.SetInitializer<testContext>(new DropCreateDatabaseIfModelChanges<testContext>());
从不创建数据库
Database.SetInitializer<testContext>(null);