C#常用操作

From , 2 Years ago, written in C#, viewed 207 times.
URL https://pastebin.vip/view/fa612be4
  1. 1. StreamWriter - 文件写入类
  2. StreamWriter s = new StreamWriter(address + "/Menu.ini", true);
  3. s.WriteLine(openFileDialog1.FileName);
  4. s.Flush();
  5. s.Close();
  6.  
  7. 2. StreamReader - 文件读取类
  8. StreamReader sr = new StreamReader(address + "/Menu.ini");
  9. while (sr.Peek()>=0)
  10. {
  11.      string str = sr.ReadLine();
  12. }
  13. sr.Close();
  14.  
  15. 3. Image - 图像类
  16. Image p = Image.FromFile("/背景图片.jpg");
  17. Form f = new Form(); // 创建MID窗口
  18. f.MdiParent = this; // 设置父窗口
  19. f.BackgroundImage = p; // 设置MDI窗口的背景图
  20. f.Show(); // 显示MDI窗口
  21.  
  22. 4. Bitmap - 位图类
  23. // 创建位图, Bitmap类继承于Image类
  24. Bitmap bit;
  25. bit = new Bitmap("heart.bmp");
  26. bit.MakeTransparent(Color.White); // 设置透明色
  27.  
  28. protected override void OnPaint(PaintEventArgs e)
  29. {
  30. // 在窗口上画图
  31. e.Graphics.DrawImage((Image)bit, new Point(0, 0));
  32. }
  33.  
  34. 5. this.Opacity - 控件的不透明度
  35. // 控制控件透明程度,很有用。
  36.  
  37. 6. C#中导入Dll文件中的API
  38. [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
  39. public static extern bool FlashWindow(IntPtr handle, bool bInvert);
  40.  
  41. 7. 隐藏标题栏
  42. this.ControlBox = false;
  43.  
  44. 8. 窗口始终处于最上面
  45. this.TopMost = ture;
  46.  
  47. 9. Screen - 桌面类
  48. Screen.PrimaryScreen.WorkingArea.Height // 桌面的高
  49. Screen.PrimaryScreen.WorkingArea.Width // 桌面的宽
  50. Screen.PrimaryScreen.BitsPerPixel   // 桌面的位深
  51.  
  52.  
  53. 10. 基本绘图
  54. Graphics graphics;
  55. Pen myPen = new Pen(Color.Blue, 2);
  56.  
  57. // 画线
  58. graphics = this.CreateGraphics();
  59. graphics.DrawLine(myPen, 30, 60, 150, 60);
  60.  
  61. // 画矩形
  62. graphics = this.CreateGraphics();
  63. graphics.DrawRectangle(myPen, 30, 80, 120, 50);
  64.  
  65. // 画椭圆
  66. graphics = this.CreateGraphics();
  67. Rectangle myRectangle = new Rectangle(160, 70, 100, 60);
  68. graphics.DrawEllipse(myPen, myRectangle);
  69.  
  70. 11. 获得鼠标在窗口中的坐标
  71. Cursor.Clip = new Rectangle(this.Location, this.Size);
  72. label1.Text = "当前鼠标的位置为:" + Cursor.Position;
  73.  
  74. 12. 判断键盘
  75. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  76. {
  77. const int WM_KEYDOWN = 0x100;
  78. const int WM_SYSKEYDOWN = 0x104;
  79. string strInfo = string.Empty;
  80. if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
  81. {
  82.    switch (keyData)
  83.    {
  84.     case Keys.Down:
  85.     strInfo = "Down Key";
  86.     break;
  87.     case Keys.Up:
  88.     strInfo = "Up Key";
  89.     break;
  90.     case Keys.Left:
  91.     strInfo = "Left Key";
  92.     break;
  93.     case Keys.Right:
  94.     strInfo = "Right Key";
  95.     break;
  96.     case Keys.Home:
  97.     strInfo = "Home Key";
  98.     break;
  99.     case Keys.End:
  100.     strInfo = "End Key";
  101.     break;
  102.    }
  103.    MessageBox.Show(strInfo, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  104. }
  105. return base.ProcessCmdKey(ref msg, keyData);
  106. }
  107.  
  108. 13. 控制远程计算机
  109. //首先添加对 System.Management的引用
  110. private void CloseComputer(string strname,string strpwd,string ip,string doinfo)
  111. {
  112. ConnectionOptions op = new ConnectionOptions ( ) ;
  113. op.Username =strname;//''或者你的帐号(注意要有管理员的权限)
  114. op.Password = strpwd; //''你的密码
  115. ManagementScope scope = new ManagementScope("////" + ip + "//root//cimv2:Win32_Service", op);
  116. try
  117. {
  118.    scope.Connect ( ) ;
  119.    System.Management.ObjectQuery oq = new System.Management.ObjectQuery ( "SELECT * FROM Win32_OperatingSystem" ) ;
  120.    ManagementObjectSearcher query1 = new ManagementObjectSearcher (scope,oq) ;
  121.    //得到WMI控制
  122.    ManagementObjectCollection queryCollection1 = query1.Get ( ) ;
  123.    foreach ( ManagementObject mobj in queryCollection1 )
  124.    {
  125.     string [ ] str= {""} ;
  126.     mobj.InvokeMethod(doinfo, str);
  127.    }
  128.    MessageBox.Show("操作成功");
  129. }
  130. catch(Exception ey)
  131. {
  132.    MessageBox.Show(ey.Message);
  133.    //this.button1.PerformClick();
  134. }
  135. }
  136.  
  137. // 重启远程计算机
  138. CloseComputer(this.textBox2.Text, this.textBox3.Text, this.textBox1.Text, "Reboot");
  139.  
  140. // 关闭远程计算机
  141. CloseComputer(this.textBox2.Text, this.textBox3.Text, this.textBox1.Text, "Shutdown");
  142.  
  143. 14. ping的使用
  144. Ping PingInfo = new Ping();
  145. PingOptions PingOpt = new PingOptions();
  146. PingOpt.DontFragment = true;
  147. string myInfo = "hyworkhyworkhyworkhyworkhyworkhywork";
  148. byte[] bufferInfo = Encoding.ASCII.GetBytes(myInfo);
  149. int TimeOut = 120;
  150. PingReply reply = PingInfo.Send(this.textBox1.Text, TimeOut, bufferInfo, PingOpt);
  151. if (reply.Status == IPStatus.Success)
  152. {
  153. this.textBox2.Text = reply.RoundtripTime.ToString();
  154. this.textBox3.Text = reply.Options.Ttl.ToString();
  155. this.textBox4.Text = (reply.Options.DontFragment ? "发生分段" : "没有发生分段");
  156. this.textBox5.Text = reply.Buffer.Length.ToString();
  157. }
  158. else
  159. {
  160. MessageBox.Show("无法Ping通");
  161. }
  162.  
  163. 15. 检查文件是否存在
  164. public int CheckFileExit(string ObjFilePath)
  165. {
  166. if (File.Exists(ObjFilePath))
  167.    return 0;
  168. else
  169.    return -1;
  170. }
  171.  
  172. //csharp/1149

Reply to "C#常用操作"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website