C#常用操作
发布时间:2020-12-15 17:54:35 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 1. StreamWriter - 文件写入类StreamWriter s = new StreamWriter(address + "/Menu.ini",true);s.WriteLine(openFileDialog1.FileName);s.Flush();s.
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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,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,"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; } 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |