C#常用操作
1. StreamWriter - 文件寫入類 StreamWriter s = new StreamWriter(address + "/Menu.ini", true); s.WriteLine(openFileDialog1.FileName); s.Flush(); s.Close();
StreamReader - 文件讀取類 StreamReader sr = new StreamReader(address + "/Menu.ini"); while (sr.Peek()>=0) { string str = sr.ReadLine(); } sr.Close();
Image - 圖像類 Image p = Image.FromFile("/背景圖片.jpg"); Form f = new Form(); // 創建MID窗口 f.MdiParent = this; // 設置父窗口 f.BackgroundImage = p; // 設置MDI窗口的背景圖 f.Show(); // 顯示MDI窗口
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)); }
this.Opacity - 控件的不透明度 // 控制控件透明程度,很有用。
C#中導入Dll文件中的API [System.Runtime.InteropServices.DllImportAttribute("user32.dll")] public static extern bool FlashWindow(IntPtr handle, bool bInvert);
隱藏標題欄 this.ControlBox = false;
窗口始終處于最上面 this.TopMost = ture;
Screen - 桌面類 Screen.PrimaryScreen.WorkingArea.Height // 桌面的高 Screen.PrimaryScreen.WorkingArea.Width // 桌面的寬 Screen.PrimaryScreen.BitsPerPixel // 桌面的位深
- 基本繪圖 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);
獲得鼠標在窗口中的坐標 Cursor.Clip = new Rectangle(this.Location, this.Size); label1.Text = "當前鼠標的位置為:" + Cursor.Position;
判斷鍵盤 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); }
控制遠程計算機 //首先添加對 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");
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通"); }
檢查文件是否存在 public int CheckFileExit(string ObjFilePath) { if (File.Exists(ObjFilePath)) return 0; else return -1; }</pre>