1.保存数据到指定文件
String response = "";//数据 byte[] bytes = response.getBytes();//转化Byte string filename =“asd”;//本身指定保存的文件名 FileStream fs = new FileStream(@"d:\" + filename + ".txt", FileMode.OpenOrCreate, FileAccess.Write);//本身指定路径 StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));//经过指定字符编码方式能够实现对汉字的支持,不然在用记事本打开查看会出现乱码 sw.Flush(); sw.BaseStream.Seek(0, SeekOrigin.Begin); sw.WriteLine(bytes); sw.Flush(); sw.Close();
2.Invoke.[form.show()]dom
Type type = Type.GetType("[namespace]." + form);ide
if (type != null)字体
{this
MethodInfo method = type.GetMethod("ShowDialog", BindingFlags.Public | BindingFlags.Instance, null, new Type[] { }, null);编码
object obj = type.Assembly.CreateInstance(type.FullName);spa
method.Invoke(obj, null);日志
}orm
3.时间换算事件
//这样对 long 作除法会出偏差(不能整除的时候)ip
public long getTimeInMillis()
{
return Decimal.ToInt64(Decimal.Divide(DateTime.Now.Ticks - 621355968000000000, 10000));
}
//Linux 时间是从 Epoch 开始算的,1970-01-01 00:00:00.//000 叫“Epoch”。
// 使用 DateTime.Now 的时候注意时区问题!//Java 是以 UTC 为基准的,而经查证,.NET 中与其对应的 是 DateTime.UtcNow 而非 DateTime.Now!
//因此,最后的结论就是:
return Decimal.ToInt64(Decimal.Divide(DateTime.Now.Ticks - new DateTime(1970, 1, 1, 8, 0, 0).Ticks, 10000))
//或者
return Decimal.ToInt64(Decimal.Divide(DateTime.UtcNow.Ticks - 621355968000000000, 10000));
4.随机颜色
public System.Drawing.Color GetRandomColor()
{
Random RandomNum_First = new Random((int)DateTime.Now.Ticks);
// 对于C#的随机数,没什么好说的
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks);
// 为了在白色背景上显示,尽可能生成深色
int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green;
int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return System.Drawing.Color.FromArgb(int_Red, int_Green, int_Blue);
}
5.搞不懂
**对于一些大型的项目,一般由不少个DLL文件组成,引用了这些DLL,就能访问DLL里面的类和类里面的方法。 好比,你写了一个记录日志的DLL,任何项目只要引用此DLL就能实现记录日志的功能,这个DLL文件的程序就是一个程序集。 若是你记录日志的程序集是这么定义的 namespace LogerHelper { internal class aa{ public void bb(){ return ""; } } public class Write{ public void WriteIn(string content){ class x = new aa(); x.bb();}}} 当另外一个项目引用了此DLL 它能够这么访问 LogerHelper.Write x = new LogerHelper.Write(); x.WriteIn(""); 但不能够这么访问 LogerHelper.aa x = new LogerHelper.aa(); x.bb(); 这就叫,只能在程序集中访问
6.枚举+随机
class Program{
static void Main(string[] args){
Color[] colors = Enum.GetValues(typeof(Color)) as Color[];
Random random = new Random();
Color color = colors[random.Next(0, colors.Length)];}}
internal enum Color{
White,Black,Red,Green,Pink}
7.threadsleep
using System.Threading; //导入命名空间,类Thread就在此空间中
Thread.Sleep(10000);
8.系统字体颜色字体大小
if (MessageBox.Show(" ?", "提示", MessageBoxButtons.YesNo) ==DialogResult.Yes) {}
FontFamily获取:
//前台有个familyList(DropDownList控件)
for(int i=0;i<FontFamily.Families.Length;i++)
{
familyList.Items.Add(FontFamily.Families[i].Name);
}
//InstalledFontCollection
InstalledFontCollection ifc=new InstalledFontCollection();
foreach(FontFamily ff in ifc.Families)
{
familyList2.Items.Add(ff.Name);
}
获取系统已安装的颜色:
//System.Drawing.KnownColor
string[] colors=Enum.GetNames(typeof(System.Drawing.KnownColor);
foreach(string color in colors)
{
ListItem list=new ListItem(color);
list.Attributes.Add("style","color:"+color);
colorList.Items.Add(list);
}
获取字体大小:
//System.Web.UI.WebControls.FontSize
string[] sizes=Enum.GetName(typeof(System.Web.UI.WebControls.FontSize));
foreach(string size in sizes)
{
sizeList.Items.Add(size);
}
9.快捷键
1、 C# button快捷键之第一种:Alt + *(按钮快捷键)
在你们给button、label、menuStrip等控件设置Text属性时在名字后边加&键名就能够了,好比button1.text= "肯定(&O)"。就会有快捷键了,这时候按Alt+O就能够执行按钮单击事件。
2、C# button快捷键之第二种:Ctrl+*及其余组合键
在WinForm中设置要使用组合键的窗体的KeyPreview(向窗体注册键盘事件)属性为True;
而后使用窗体的KeyDown事件(在首次按下某个键时发生).
C# button快捷键实例代码以下:
private void ***_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F && e.Control)
{
button1.PerformClick(); //执行单击button1的动做
}
}
此处注意:
一、***表明窗体名称,看一下 ”Keys”的枚举参数,以实现本身的须要
二、还有一个问题,当使用Ctrl + *快捷键时,对于焦点在可写的控件(如TextBox)上时,可能会将* 键值同时输入,则须要加另外一句话将Handled设置为true,以取消 KeyPress 事件。即:
private void ***_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F && e.Control)
{
e.Handled = true; //将Handled设置为true,指示已经处理过KeyPress事件
button1.PerformClick();
}
}
3、C# button快捷键之第三种方法
仍是以button为例。给form添加一个contextMenuStrip1,将其邦定到button上,假设为button1。给 contextMenuStrip1添加一个item,而后为它设置快捷键(就是你想加在button上的快捷键),而且将它的Visible属性设为 false。这样,C# button快捷键设置成功。
4、C# button快捷键之第四种方法
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Escape))
{
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}
10.DataTable Set PrimaryKey:
if (dt.PrimaryKey == null || dt.PrimaryKey.Length == 0)
{
dt.PrimaryKey = new DataColumn[] { dt.Columns["PK_ColunmeName"] };
}
DataRow myDataRow= myDataSet.Tables["TableName"].Rows.Find("primary key data");
11.Find the ContextMenuStrip(Contrl) ‘s ParentControl
stringcontrol_name= (senderasContextMenuStrip).SourceControl.Name;