| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
bdnp
10年前发布

C#常用操作

1. StreamWriter - 文件写入类  StreamWriter s = new StreamWriter(address + "/Menu.ini", true);  s.WriteLine(openFileDialog1.FileName);  s.Flush();  s.Close();     2. StreamReader - 文件读取类  StreamReader sr = new StreamReader(address + "/Menu.ini");  while (sr.Peek()>=0)  {       string str = sr.ReadLine();  }  sr.Close();     3. Image - 图像类  Image p = Image.FromFile("/背景图片.jpg");  Form f = new Form(); // 创建MID窗口  f.MdiParent = this; // 设置父窗口  f.BackgroundImage = p; // 设置MDI窗口的背景图  f.Show(); // 显示MDI窗口     4. Bitmap - 位图类  // 创建位图, Bitmap类继承于Image类  Bitmap bit;  bit = new Bitmap("heart.bmp");  bit.MakeTransparent(Color.White); // 设置透明色     protected override void OnPaint(PaintEventArgs e)  {  // 在窗口上画图  e.Graphics.DrawImage((Image)bit, new Point(0, 0));  }     5. this.Opacity - 控件的不透明度  // 控制控件透明程度,很有用。     6. C#中导入Dll文件中的API  [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]  public static extern bool FlashWindow(IntPtr handle, bool bInvert);     7. 隐藏标题栏  this.ControlBox = false;     8. 窗口始终处于最上面  this.TopMost = ture;     9. Screen - 桌面类  Screen.PrimaryScreen.WorkingArea.Height // 桌面的高  Screen.PrimaryScreen.WorkingArea.Width // 桌面的宽  Screen.PrimaryScreen.BitsPerPixel   // 桌面的位深        10. 基本绘图  Graphics graphics;  Pen myPen = new Pen(Color.Blue, 2);     // 画线  graphics = this.CreateGraphics();  graphics.DrawLine(myPen, 30, 60, 150, 60);     // 画矩形  graphics = this.CreateGraphics();  graphics.DrawRectangle(myPen, 30, 80, 120, 50);     // 画椭圆  graphics = this.CreateGraphics();  Rectangle myRectangle = new Rectangle(160, 70, 100, 60);  graphics.DrawEllipse(myPen, myRectangle);     11. 获得鼠标在窗口中的坐标  Cursor.Clip = new Rectangle(this.Location, this.Size);  label1.Text = "当前鼠标的位置为:" + Cursor.Position;     12. 判断键盘  protected override bool ProcessCmdKey(ref Message msg, Keys keyData)  {  const int WM_KEYDOWN = 0x100;  const int WM_SYSKEYDOWN = 0x104;  string strInfo = string.Empty;  if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))  {     switch (keyData)     {      case Keys.Down:      strInfo = "Down Key";      break;      case Keys.Up:      strInfo = "Up Key";      break;      case Keys.Left:      strInfo = "Left Key";      break;      case Keys.Right:      strInfo = "Right Key";      break;      case Keys.Home:      strInfo = "Home Key";      break;      case Keys.End:      strInfo = "End Key";      break;     }     MessageBox.Show(strInfo, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);  }  return base.ProcessCmdKey(ref msg, keyData);  }     13. 控制远程计算机  //首先添加对 System.Management的引用  private void CloseComputer(string strname,string strpwd,string ip,string doinfo)  {  ConnectionOptions op = new ConnectionOptions ( ) ;  op.Username =strname;//''或者你的帐号(注意要有管理员的权限)  op.Password = strpwd; //''你的密码  ManagementScope scope = new ManagementScope("////" + ip + "//root//cimv2:Win32_Service", op);  try  {     scope.Connect ( ) ;     System.Management.ObjectQuery oq = new System.Management.ObjectQuery ( "SELECT * FROM Win32_OperatingSystem" ) ;     ManagementObjectSearcher query1 = new ManagementObjectSearcher (scope,oq) ;     //得到WMI控制     ManagementObjectCollection queryCollection1 = query1.Get ( ) ;     foreach ( ManagementObject mobj in queryCollection1 )     {      string [ ] str= {""} ;      mobj.InvokeMethod(doinfo, str);     }     MessageBox.Show("操作成功");  }  catch(Exception ey)  {     MessageBox.Show(ey.Message);     //this.button1.PerformClick();  }  }     // 重启远程计算机  CloseComputer(this.textBox2.Text, this.textBox3.Text, this.textBox1.Text, "Reboot");     // 关闭远程计算机  CloseComputer(this.textBox2.Text, this.textBox3.Text, this.textBox1.Text, "Shutdown");     14. ping的使用  Ping PingInfo = new Ping();  PingOptions PingOpt = new PingOptions();  PingOpt.DontFragment = true;  string myInfo = "hyworkhyworkhyworkhyworkhyworkhywork";  byte[] bufferInfo = Encoding.ASCII.GetBytes(myInfo);  int TimeOut = 120;  PingReply reply = PingInfo.Send(this.textBox1.Text, TimeOut, bufferInfo, PingOpt);  if (reply.Status == IPStatus.Success)  {  this.textBox2.Text = reply.RoundtripTime.ToString();  this.textBox3.Text = reply.Options.Ttl.ToString();  this.textBox4.Text = (reply.Options.DontFragment ? "发生分段" : "没有发生分段");  this.textBox5.Text = reply.Buffer.Length.ToString();  }  else  {  MessageBox.Show("无法Ping通");  }     15. 检查文件是否存在  public int CheckFileExit(string ObjFilePath)  {  if (File.Exists(ObjFilePath))     return 0;  else     return -1;  }