C#常用操作

来源:互联网 发布:mac进不了安全模式 编辑:程序博客网 时间:2024/05/21 01:58
//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;}

原创粉丝点击