C#常用操作

C#常用操作

 

http://www.oschina.net/code/snippet_119226_5833

 

[代码] [C#]代码

001 1. StreamWriter - 文件写入类
002 StreamWriter s = new StreamWriter(address + "/Menu.ini"true);
003 s.WriteLine(openFileDialog1.FileName);
004 s.Flush();
005 s.Close();
006  
007 2. StreamReader - 文件读取类
008 StreamReader sr = new StreamReader(address + "/Menu.ini");
009 while (sr.Peek()>=0)
010 {
011      string str = sr.ReadLine();
012 }
013 sr.Close();
014  
015 3. Image - 图像类
016 Image p = Image.FromFile("/背景图片.jpg");
017 Form f = new Form(); // 创建MID窗口
018 f.MdiParent = this// 设置父窗口
019 f.BackgroundImage = p; // 设置MDI窗口的背景图
020 f.Show(); // 显示MDI窗口
021  
022 4. Bitmap - 位图类
023 // 创建位图, Bitmap类继承于Image类
024 Bitmap bit;
025 bit = new Bitmap("heart.bmp");
026 bit.MakeTransparent(Color.White); // 设置透明色
027  
028 protected override void OnPaint(PaintEventArgs e)
029 {
030 // 在窗口上画图
031 e.Graphics.DrawImage((Image)bit, new Point(0, 0));
032 }
033  
034 5. this.Opacity - 控件的不透明度
035 // 控制控件透明程度,很有用。
036  
037 6. C#中导入Dll文件中的API
038 [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
039 public static extern bool FlashWindow(IntPtr handle, bool bInvert);
040  
041 7. 隐藏标题栏
042 this.ControlBox = false;
043  
044 8. 窗口始终处于最上面
045 this.TopMost = ture;
046  
047 9. Screen - 桌面类
048 Screen.PrimaryScreen.WorkingArea.Height // 桌面的高
049 Screen.PrimaryScreen.WorkingArea.Width // 桌面的宽
050 Screen.PrimaryScreen.BitsPerPixel   // 桌面的位深
051  
052  
053 10. 基本绘图
054 Graphics graphics;
055 Pen myPen = new Pen(Color.Blue, 2);
056  
057 // 画线
058 graphics = this.CreateGraphics();
059 graphics.DrawLine(myPen, 30, 60, 150, 60);
060  
061 // 画矩形
062 graphics = this.CreateGraphics();
063 graphics.DrawRectangle(myPen, 30, 80, 120, 50);
064  
065 // 画椭圆
066 graphics = this.CreateGraphics();
067 Rectangle myRectangle = new Rectangle(160, 70, 100, 60);
068 graphics.DrawEllipse(myPen, myRectangle);
069  
070 11. 获得鼠标在窗口中的坐标
071 Cursor.Clip = new Rectangle(this.Location, this.Size);
072 label1.Text = "当前鼠标的位置为:" + Cursor.Position;
073  
074 12. 判断键盘
075 protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
076 {
077 const int WM_KEYDOWN = 0x100;
078 const int WM_SYSKEYDOWN = 0x104;
079 string strInfo = string.Empty;
080 if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
081 {
082    switch (keyData)
083    {
084     case Keys.Down:
085     strInfo = "Down Key";
086     break;
087     case Keys.Up:
088     strInfo = "Up Key";
089     break;
090     case Keys.Left:
091     strInfo = "Left Key";
092     break;
093     case Keys.Right:
094     strInfo = "Right Key";
095     break;
096     case Keys.Home:
097     strInfo = "Home Key";
098     break;
099     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,stringdoinfo)
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 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值