经常用到的,转一个

转一下:)

  1 None.gif
  2 None.gif一、从控制台读取东西代码片断:
  3 None.gif using  System;
  4 None.gif
  5 None.gif class  TestReadConsole
  6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  7InBlock.gif    public static void Main()
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
  9InBlock.gif        Console.Write(Enter your name:);
 10InBlock.gif        string strName = Console.ReadLine();
 11InBlock.gif        Console.WriteLine( Hi + strName);
 12ExpandedSubBlockEnd.gif    }

 13ExpandedBlockEnd.gif}

 14 None.gif二、读文件代码片断:
 15 None.gif using  System; 
 16 None.gif using  System.IO; 
 17 None.gif
 18 None.gif public   class  TestReadFile 
 19 ExpandedBlockStart.gifContractedBlock.gif dot.gif
 20InBlock.gif    public static void Main(String[] args) 
 21ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
 22InBlock.gif        // Read text file C:\temp\test.txt 
 23InBlock.gif        FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.Open, FileAccess.Read); 
 24InBlock.gif        StreamReader sr = new StreamReader(fs);  
 25InBlock.gif        
 26InBlock.gif        String line=sr.ReadLine();
 27InBlock.gif        while (line!=null)
 28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 29InBlock.gif            Console.WriteLine(line);
 30InBlock.gif            line=sr.ReadLine();
 31ExpandedSubBlockEnd.gif        }
   
 32InBlock.gif        
 33InBlock.gif        sr.Close();
 34InBlock.gif        fs.Close();
 35ExpandedSubBlockEnd.gif    }
 
 36ExpandedBlockEnd.gif}
 
 37 None.gif三、写文件代码:
 38 None.gif using  System; 
 39 None.gif using  System.IO; 
 40 None.gif
 41 None.gif public   class  TestWriteFile 
 42 ExpandedBlockStart.gifContractedBlock.gif dot.gif
 43InBlock.gif    public static void Main(String[] args) 
 44ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
 45InBlock.gif        // Create a text file C:\temp\test.txt 
 46InBlock.gif        FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.OpenOrCreate, FileAccess.Write); 
 47InBlock.gif        StreamWriter sw = new StreamWriter(fs); 
 48InBlock.gif        // Write to the file using StreamWriter class 
 49InBlock.gif        sw.BaseStream.Seek(0, SeekOrigin.End); 
 50InBlock.gif        sw.WriteLine( First Line ); 
 51InBlock.gif        sw.WriteLine( Second Line); 
 52InBlock.gif        sw.Flush(); 
 53ExpandedSubBlockEnd.gif    }
 
 54ExpandedBlockEnd.gif}
 
 55 None.gif四、拷贝文件:
 56 None.gif using  System;
 57 None.gif using  System.IO;
 58 None.gif
 59 None.gif class  TestCopyFile
 60 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 61InBlock.gif    public static void Main()
 62ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 63InBlock.gif        File.Copy(c:\\temp\\source.txt, C:\\temp\\dest.txt );  
 64ExpandedSubBlockEnd.gif    }

 65ExpandedBlockEnd.gif}

 66 None.gif五、移动文件:
 67 None.gif using  System;
 68 None.gif using  System.IO;
 69 None.gif
 70 None.gif class  TestMoveFile
 71 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 72InBlock.gif    public static void Main()
 73ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 74InBlock.gif        File.Move(c:\\temp\\abc.txt, C:\\temp\\def.txt );  
 75ExpandedSubBlockEnd.gif    }

 76ExpandedBlockEnd.gif}

 77 None.gif六、使用计时器:
 78 None.gif using  System;
 79 None.gif using  System.Timers;
 80 None.gif
 81 None.gif class  TestTimer
 82 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 83InBlock.gif    public static void Main()
 84ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 85InBlock.gif        Timer timer = new Timer();
 86InBlock.gif        timer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
 87InBlock.gif        timer.Interval = 1000;
 88InBlock.gif        timer.Start();
 89InBlock.gif        timer.Enabled = true;
 90InBlock.gif
 91InBlock.gif        while ( Console.Read() != 'q' )
 92ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 93InBlock.gif             //-------------
 94ExpandedSubBlockEnd.gif        }

 95ExpandedSubBlockEnd.gif    }

 96InBlock.gif    public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
 97ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        Console.Write(\rdot.gif{0}, DateTime.Now);
 99ExpandedSubBlockEnd.gif    }

100ExpandedBlockEnd.gif}

101 None.gif七、调用外部程序:
102 None.gif class  Test 
103 ExpandedBlockStart.gifContractedBlock.gif dot.gif
104InBlock.gif    static void Main(string[] args) 
105ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
106InBlock.gif        System.Diagnostics.Process.Start(notepad.exe); 
107ExpandedSubBlockEnd.gif    }
 
108ExpandedBlockEnd.gif}

109 None.gif
110 None.gifADO.NET方面的:
111 None.gif八、连接Access数据库:
112 None.gif using  System;
113 None.gif using  System.Data;
114 None.gif using  System.Data.OleDb;
115 None.gif
116 None.gif class  TestADO
117 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
118InBlock.gif    static void Main(string[] args)
119ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
120InBlock.gif        string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.mdb;
121InBlock.gif        string strSQL = SELECT * FROM employees ;
122InBlock.gif
123InBlock.gif        OleDbConnection conn = new OleDbConnection(strDSN);
124InBlock.gif        OleDbCommand cmd = new OleDbCommand( strSQL, conn );
125InBlock.gif        OleDbDataReader reader = null;
126InBlock.gif        try
127ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
128InBlock.gif            conn.Open();
129InBlock.gif            reader = cmd.ExecuteReader();
130InBlock.gif            while (reader.Read() )
131ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
132ExpandedSubBlockStart.gifContractedSubBlock.gif                Console.WriteLine(First Name:dot.gif{0}, Last Name:dot.gif{1}, reader[FirstName], reader[LastName]);
133ExpandedSubBlockEnd.gif            }

134ExpandedSubBlockEnd.gif        }

135InBlock.gif        catch (Exception e)
136ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
137InBlock.gif            Console.WriteLine(e.Message);
138ExpandedSubBlockEnd.gif        }

139InBlock.gif        finally
140ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
141InBlock.gif            conn.Close();
142ExpandedSubBlockEnd.gif        }

143ExpandedSubBlockEnd.gif    }

144ExpandedBlockEnd.gif}
 
145 None.gif九、连接SQL Server数据库:
146 None.gif using  System;
147 None.gif using  System.Data.SqlClient;
148 None.gif
149 None.gif public   class  TestADO
150 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
151InBlock.gif    public static void Main()
152ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
153InBlock.gif        SqlConnection conn = new SqlConnection(Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs);
154InBlock.gif        SqlCommand  cmd = new SqlCommand(SELECT * FROM employees, conn);
155InBlock.gif        try
156ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{        
157InBlock.gif            conn.Open();
158InBlock.gif
159InBlock.gif            SqlDataReader reader = cmd.ExecuteReader();            
160InBlock.gif            while (reader.Read())
161ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
162ExpandedSubBlockStart.gifContractedSubBlock.gif                Console.WriteLine(First Name: dot.gif{0}, Last Name: dot.gif{1}, reader.GetString(0), reader.GetString(1));
163ExpandedSubBlockEnd.gif            }

164InBlock.gif        
165InBlock.gif            reader.Close();
166InBlock.gif            conn.Close();
167ExpandedSubBlockEnd.gif        }

168InBlock.gif        catch(Exception e)
169ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
170ExpandedSubBlockStart.gifContractedSubBlock.gif            Console.WriteLine(Exception Occured -->> dot.gif{0},e);
171ExpandedSubBlockEnd.gif        }
        
172ExpandedSubBlockEnd.gif    }

173ExpandedBlockEnd.gif}

174 None.gif十、从SQL内读数据到XML:
175 None.gif using  System;
176 None.gif using  System.Data;
177 None.gif using  System.Xml;
178 None.gif using  System.Data.SqlClient; 
179 None.gif using  System.IO; 
180 None.gif
181 None.gif public   class  TestWriteXML
182 ExpandedBlockStart.gifContractedBlock.gif dot.gif
183InBlock.gif    public static void Main()
184ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
185InBlock.gif
186InBlock.gif        String strFileName=c:/temp/output.xml;
187InBlock.gif
188InBlock.gif        SqlConnection conn = new SqlConnection(server=localhost;uid=sa;pwd=;database=db);
189InBlock.gif
190InBlock.gif        String strSql = SELECT FirstName, LastName FROM employees; 
191InBlock.gif
192InBlock.gif        SqlDataAdapter adapter = new SqlDataAdapter(); 
193InBlock.gif
194InBlock.gif        adapter.SelectCommand = new SqlCommand(strSql,conn);
195InBlock.gif
196InBlock.gif        // Build the DataSet
197InBlock.gif        DataSet ds = new DataSet();
198InBlock.gif
199InBlock.gif        adapter.Fill(ds, employees);
200InBlock.gif
201InBlock.gif        // Get a FileStream object
202InBlock.gif        FileStream fs = new FileStream(strFileName,FileMode.OpenOrCreate,FileAccess.Write);
203InBlock.gif
204InBlock.gif        // Apply the WriteXml method to write an XML document
205InBlock.gif        ds.WriteXml(fs);
206InBlock.gif
207InBlock.gif        fs.Close();
208InBlock.gif
209ExpandedSubBlockEnd.gif    }

210ExpandedBlockEnd.gif}

211 None.gif十一、用ADO添加数据到数据库中:
212 None.gif using  System;
213 None.gif using  System.Data;   
214 None.gif using  System.Data.OleDb;   
215 None.gif
216 None.gif class  TestADO
217 ExpandedBlockStart.gifContractedBlock.gif dot.gif {  
218InBlock.gif    static void Main(string[] args)  
219ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{  
220InBlock.gif        string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;  
221InBlock.gif        string strSQL = INSERT INTO Employee(FirstName, LastName) VALUES('FirstName''LastName') ;  
222InBlock.gif                   
223InBlock.gif        // create Objects of ADOConnection and ADOCommand   
224InBlock.gif        OleDbConnection conn = new OleDbConnection(strDSN);  
225InBlock.gif        OleDbCommand cmd = new OleDbCommand( strSQL, conn );  
226InBlock.gif        try  
227ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  
228InBlock.gif            conn.Open();  
229InBlock.gif            cmd.ExecuteNonQuery();  
230ExpandedSubBlockEnd.gif        }
  
231InBlock.gif        catch (Exception e)  
232ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  
233ExpandedSubBlockStart.gifContractedSubBlock.gif            Console.WriteLine(Oooops. I did it again:\ndot.gif{0}, e.Message);  
234ExpandedSubBlockEnd.gif        }
  
235InBlock.gif        finally  
236ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  
237InBlock.gif            conn.Close();  
238ExpandedSubBlockEnd.gif        }
          
239ExpandedSubBlockEnd.gif    }
 
240ExpandedBlockEnd.gif}
  
241 None.gif十二、使用OLEConn连接数据库:
242 None.gif using  System;
243 None.gif using  System.Data;   
244 None.gif using  System.Data.OleDb;   
245 None.gif
246 None.gif class  TestADO
247 ExpandedBlockStart.gifContractedBlock.gif dot.gif {  
248InBlock.gif    static void Main(string[] args)  
249ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{  
250InBlock.gif        string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;  
251InBlock.gif        string strSQL = SELECT * FROM employee ;  
252InBlock.gif
253InBlock.gif        OleDbConnection conn = new OleDbConnection(strDSN);
254InBlock.gif        OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn ); 
255InBlock.gif
256InBlock.gif        conn.Open();
257InBlock.gif        DataSet ds = new DataSet();
258InBlock.gif        cmd.Fill( ds, employee );
259InBlock.gif        DataTable dt = ds.Tables[0];
260InBlock.gif
261InBlock.gif        foreach( DataRow dr in dt.Rows )
262ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
263InBlock.gif            Console.WriteLine(First name: + dr[FirstName].ToString() + Last name: + dr[LastName].ToString());
264ExpandedSubBlockEnd.gif        }

265InBlock.gif        conn.Close();  
266ExpandedSubBlockEnd.gif    }
 
267ExpandedBlockEnd.gif}
  
268 None.gif十三、读取表的属性:
269 None.gif using  System;
270 None.gif using  System.Data;   
271 None.gif using  System.Data.OleDb;   
272 None.gif
273 None.gif class  TestADO
274 ExpandedBlockStart.gifContractedBlock.gif dot.gif {  
275InBlock.gif    static void Main(string[] args)  
276ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{  
277InBlock.gif        string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;  
278InBlock.gif        string strSQL = SELECT * FROM employee ;  
279InBlock.gif
280InBlock.gif        OleDbConnection conn = new OleDbConnection(strDSN);
281InBlock.gif        OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn ); 
282InBlock.gif
283InBlock.gif        conn.Open();
284InBlock.gif        DataSet ds = new DataSet();
285InBlock.gif        cmd.Fill( ds, employee );
286InBlock.gif        DataTable dt = ds.Tables[0];
287InBlock.gif
288InBlock.gif        Console.WriteLine(Field Name DataType Unique AutoIncrement AllowNull);
289InBlock.gif        Console.WriteLine(==================================================================);
290InBlock.gif        foreach( DataColumn dc in dt.Columns )
291ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
292InBlock.gif            Console.WriteLine(dc.ColumnName+ , +dc.DataType + ,+dc.Unique + ,+dc.AutoIncrement+ ,+dc.AllowDBNull );
293ExpandedSubBlockEnd.gif        }

294InBlock.gif        conn.Close();  
295ExpandedSubBlockEnd.gif    }
 
296ExpandedBlockEnd.gif}
 
297 None.gif
298 None.gifASP.NET方面的
299 None.gif十四、一个ASP.NET程序:
300 None.gif <% @ Page Language = C#  %>
301 None.gif < script runat = server >
302 None.gif   
303 None.gif     void  Button1_Click(Object sender, EventArgs e) 
304 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
305InBlock.gif        Label1.Text=TextBox1.Text;
306ExpandedBlockEnd.gif    }

307 None.gif
308 None.gif </ script >
309 None.gif < html >
310 None.gif < head >
311 None.gif </ head >
312 None.gif < body >
313 None.gif     < form runat = server >
314 None.gif         < p >
315 None.gif             < br  />
316 None.gif            Enter your name:  < asp:TextBox id = TextBox1 runat = server ></ asp:TextBox >
317 None.gif         </ p >
318 None.gif         < p >
319 None.gif             < b >< asp:Label id = Label1 runat = server Width = 247px ></ asp:Label ></ b >
320 None.gif         </ p >
321 None.gif         < p >
322 None.gif             < asp:Button id = Button1 onclick = Button1_Click runat = server Text = Submit ></ asp:Button >
323 None.gif         </ p >
324 None.gif     </ form >
325 None.gif </ body >
326 None.gif </ html >
327 None.gif
328 None.gifWinForm开发:
329 None.gif十五、一个简单的WinForm程序:
330 None.gif using  System;
331 None.gif using  System.Drawing;
332 None.gif using  System.Collections;
333 None.gif using  System.ComponentModel;
334 None.gif using  System.Windows.Forms;
335 None.gif using  System.Data;
336 None.gif
337 None.gif
338 None.gif public   class  SimpleForm : System.Windows.Forms.Form
339 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
340InBlock.gif
341InBlock.gif    private System.ComponentModel.Container components = null;
342InBlock.gif    private System.Windows.Forms.Button button1;
343InBlock.gif    private System.Windows.Forms.TextBox textBox1;
344InBlock.gif    public SimpleForm()
345ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
346InBlock.gif        InitializeComponent();
347ExpandedSubBlockEnd.gif    }

348InBlock.gif
349InBlock.gif    protected override void Dispose( bool disposing )
350ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
351InBlock.gif        if( disposing )
352ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
353InBlock.gif            if (components != null)
354ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
355InBlock.gif                components.Dispose();
356ExpandedSubBlockEnd.gif            }

357ExpandedSubBlockEnd.gif        }

358InBlock.gif        base.Dispose( disposing );
359ExpandedSubBlockEnd.gif    }

360InBlock.gif
361ContractedSubBlock.gifExpandedSubBlockStart.gif    Windows Form Designer generated code#region Windows Form Designer generated code
362InBlock.gif    private void InitializeComponent()
363ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
364InBlock.gif
365InBlock.gif        this.components = new System.ComponentModel.Container();
366InBlock.gif        this.Size = new System.Drawing.Size(300,300);
367InBlock.gif        this.Text = Form1;
368InBlock.gif
369InBlock.gif        this.button1 = new System.Windows.Forms.Button();
370InBlock.gif        this.textBox1 = new System.Windows.Forms.TextBox();
371InBlock.gif        this.SuspendLayout(); 
372InBlock.gif    //
373InBlock.gif    // button1
374InBlock.gif    //
375InBlock.gif
376InBlock.gif    this.button1.Location = new System.Drawing.Point(816);
377InBlock.gif    this.button1.Name = button1;
378InBlock.gif    this.button1.Size = new System.Drawing.Size(8024);
379InBlock.gif    this.button1.TabIndex = 0;
380InBlock.gif    this.button1.Text = button1;
381InBlock.gif
382InBlock.gif    //
383InBlock.gif    // textBox1
384InBlock.gif    //
385InBlock.gif    this.textBox1.Location = new System.Drawing.Point(11216);
386InBlock.gif    this.textBox1.Name = textBox1;
387InBlock.gif    this.textBox1.Size = new System.Drawing.Size(16020);
388InBlock.gif    this.textBox1.TabIndex = 1;
389InBlock.gif    this.textBox1.Text = textBox1;
390InBlock.gif    //
391InBlock.gif    // Form1
392InBlock.gif    //
393InBlock.gif
394InBlock.gif    this.AutoScaleBaseSize = new System.Drawing.Size(513);
395InBlock.gif    this.ClientSize = new System.Drawing.Size(292273);
396ExpandedSubBlockStart.gifContractedSubBlock.gif    this.Controls.AddRange(new System.Windows.Forms.Control[] dot.gif{
397InBlock.gif    this.textBox1,
398ExpandedSubBlockEnd.gif    this.button1}
);
399InBlock.gif    this.Name = Form1;
400InBlock.gif    this.Text = Form1;
401InBlock.gif    this.ResumeLayout(false); 
402InBlock.gif
403ExpandedSubBlockEnd.gif    }

404ExpandedSubBlockEnd.gif    #endregion

405InBlock.gif
406InBlock.gif    [STAThread]
407InBlock.gif    static void Main()
408ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
409InBlock.gif        Application.Run(new SimpleForm());
410ExpandedSubBlockEnd.gif    }
 
411ExpandedBlockEnd.gif}

412 None.gif十六、运行时显示自己定义的图标:
413 None.gif // load icon and set to form
414 None.gif System.Drawing.Icon ico  =   new  System.Drawing.Icon(@c:\temp\app.ico);
415 None.gif this .Icon  =  ico;
416 None.gif十七、添加组件到ListBox中:
417 None.gif private   void  Form1_Load( object  sender, System.EventArgs e)
418 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
419InBlock.gif    string str = First item;
420InBlock.gif    int i = 23;
421InBlock.gif    float flt = 34.98f
422InBlock.gif    listBox1.Items.Add(str);
423InBlock.gif    listBox1.Items.Add(i.ToString());
424InBlock.gif    listBox1.Items.Add(flt.ToString());
425InBlock.gif    listBox1.Items.Add(Last Item in the List Box);
426ExpandedBlockEnd.gif}
 
427 None.gif
428 None.gif网络方面的:
429 None.gif十八、取得IP地址:
430 None.gif using  System;
431 None.gif using  System.Net;
432 None.gif
433 None.gif class  GetIP
434 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
435InBlock.gif     public static void Main()
436ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
437InBlock.gif         IPHostEntry ipEntry = Dns.GetHostByName (localhost);
438InBlock.gif         IPAddress [] IpAddr = ipEntry.AddressList;
439InBlock.gif         for (int i = 0; i < IpAddr.Length; i++)
440ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif
441ExpandedSubBlockStart.gifContractedSubBlock.gif             Console.WriteLine (IP Address dot.gif{0}dot.gif{1} , i, IpAddr.ToString ());
442ExpandedSubBlockEnd.gif         }

443ExpandedSubBlockEnd.gif    }

444ExpandedBlockEnd.gif}

445 None.gif十九、取得机器名称:
446 None.gif using  System;
447 None.gif using  System.Net;
448 None.gif
449 None.gif class  GetIP
450 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
451InBlock.gif    public static void Main()
452ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
453ExpandedSubBlockStart.gifContractedSubBlock.gif          Console.WriteLine (Host name : dot.gif{0}, Dns.GetHostName());
454ExpandedSubBlockEnd.gif    }

455ExpandedBlockEnd.gif}

456 None.gif二十、发送邮件:
457 None.gif using  System;
458 None.gif using  System.Web;
459 None.gif using  System.Web.Mail;
460 None.gif
461 None.gif public   class  TestSendMail
462 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
463InBlock.gif    public static void Main()
464ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
465InBlock.gif        try
466ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
467InBlock.gif            // Construct a new mail message 
468InBlock.gif            MailMessage message = new MailMessage();
469InBlock.gif            message.From = from@domain.com;
470InBlock.gif            message.To   =  pengyun@cobainsoft.com;
471InBlock.gif            message.Cc   = ;
472InBlock.gif            message.Bcc  = ;
473InBlock.gif            message.Subject = Subject;
474InBlock.gif            message.Body = Content of message;
475InBlock.gif            
476InBlock.gif            //if you want attach file with this mail, add the line below
477InBlock.gif            message.Attachments.Add(new MailAttachment(c:\\attach.txt, MailEncoding.Base64));
478InBlock.gif  
479InBlock.gif            // Send the message
480InBlock.gif            SmtpMail.Send(message);  
481InBlock.gif            System.Console.WriteLine(Message has been sent);
482ExpandedSubBlockEnd.gif        }

483InBlock.gif
484InBlock.gif        catch(Exception ex)
485ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
486InBlock.gif            System.Console.WriteLine(ex.Message.ToString());
487ExpandedSubBlockEnd.gif        }

488InBlock.gif
489ExpandedSubBlockEnd.gif    }

490ExpandedBlockEnd.gif}

491 None.gif二十一、根据IP地址得出机器名称:
492 None.gif using  System;
493 None.gif using  System.Net;
494 None.gif
495 None.gif class  ResolveIP
496 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
497InBlock.gif     public static void Main()
498ExpandedSubBlockStart.gifContractedSubBlock.gif     dot.gif{
499InBlock.gif         IPHostEntry ipEntry = Dns.Resolve(172.29.9.9);
500ExpandedSubBlockStart.gifContractedSubBlock.gif         Console.WriteLine (Host name : dot.gif{0}, ipEntry.HostName);         
501ExpandedSubBlockEnd.gif     }

502ExpandedBlockEnd.gif}

503 None.gif
504 None.gifGDI + 方面的:
505 None.gif二十二、GDI + 入门介绍:
506 None.gif using  System;
507 None.gif using  System.Drawing;
508 None.gif using  System.Collections;
509 None.gif using  System.ComponentModel;
510 None.gif using  System.Windows.Forms;
511 None.gif using  System.Data;
512 None.gif
513 None.gif public   class  Form1 : System.Windows.Forms.Form
514 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
515InBlock.gif    private System.ComponentModel.Container components = null;
516InBlock.gif
517InBlock.gif    public Form1()
518ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
519InBlock.gif        InitializeComponent();
520ExpandedSubBlockEnd.gif    }

521InBlock.gif
522InBlock.gif    protected override void Dispose( bool disposing )
523ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
524InBlock.gif        if( disposing )
525ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
526InBlock.gif            if (components != null
527ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
528InBlock.gif                components.Dispose();
529ExpandedSubBlockEnd.gif            }

530ExpandedSubBlockEnd.gif        }

531InBlock.gif        base.Dispose( disposing );
532ExpandedSubBlockEnd.gif    }

533InBlock.gif
534ContractedSubBlock.gifExpandedSubBlockStart.gif    Windows Form Designer generated code#region Windows Form Designer generated code
535InBlock.gif    private void InitializeComponent()
536ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
537InBlock.gif        this.AutoScaleBaseSize = new System.Drawing.Size(513);
538InBlock.gif        this.ClientSize = new System.Drawing.Size(292273);
539InBlock.gif        this.Name = Form1;
540InBlock.gif        this.Text = Form1;
541InBlock.gif        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
542ExpandedSubBlockEnd.gif    }

543ExpandedSubBlockEnd.gif    #endregion

544InBlock.gif
545InBlock.gif    [STAThread]
546InBlock.gif    static void Main() 
547ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
548InBlock.gif        Application.Run(new Form1());
549ExpandedSubBlockEnd.gif    }

550InBlock.gif
551InBlock.gif    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
552ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
553InBlock.gif        Graphics g=e.Graphics;
554InBlock.gif        g.DrawLine(new Pen(Color.Blue),10,10,210,110);
555InBlock.gif        g.DrawRectangle(new Pen(Color.Red),10,10,200,100);
556InBlock.gif        g.DrawEllipse(new Pen(Color.Yellow),10,150,200,100);
557ExpandedSubBlockEnd.gif    }

558ExpandedBlockEnd.gif}

559 None.gif
560 None.gifXML方面的:
561 None.gif二十三、读取XML文件:
562 None.gif using  System;
563 None.gif using  System.Xml;  
564 None.gif
565 None.gif class  TestReadXML
566 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
567InBlock.gif    public static void Main()
568ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
569InBlock.gif        
570InBlock.gif        XmlTextReader reader  = new XmlTextReader(C:\\test.xml);
571InBlock.gif        reader.Read();
572InBlock.gif        
573InBlock.gif        while (reader.Read())
574ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
575InBlock.gif            reader.MoveToElement();
576InBlock.gif            Console.WriteLine(XmlTextReader Properties Test);
577InBlock.gif            Console.WriteLine(===================);  
578InBlock.gif
579InBlock.gif            // Read this properties of element and display them on console
580InBlock.gif            Console.WriteLine(Name: + reader.Name);
581InBlock.gif            Console.WriteLine(Base URI: + reader.BaseURI);
582InBlock.gif            Console.WriteLine(Local Name: + reader.LocalName);
583InBlock.gif            Console.WriteLine(Attribute Count: + reader.AttributeCount.ToString());
584InBlock.gif            Console.WriteLine(Depth: + reader.Depth.ToString());
585InBlock.gif            Console.WriteLine(Line Number: + reader.LineNumber.ToString());
586InBlock.gif            Console.WriteLine(Node Type: + reader.NodeType.ToString());
587InBlock.gif            Console.WriteLine(Attribute Count: + reader.Value.ToString());
588ExpandedSubBlockEnd.gif        }
        
589ExpandedSubBlockEnd.gif    }
               
590ExpandedBlockEnd.gif}
 
591 None.gif二十四、写XML文件:
592 None.gif using  System; 
593 None.gif using  System.Xml; 
594 None.gif
595 None.gif public   class  TestWriteXMLFile 
596 ExpandedBlockStart.gifContractedBlock.gif dot.gif
597InBlock.gif    public static int Main(string[] args) 
598ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
599InBlock.gif        try 
600ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  
601InBlock.gif            // Creates an XML file is not exist 
602InBlock.gif            XmlTextWriter writer = new XmlTextWriter(C:\\temp\\xmltest.xml, null); 
603InBlock.gif            // Starts a new document 
604InBlock.gif            writer.WriteStartDocument(); 
605InBlock.gif            //Write comments 
606InBlock.gif            writer.WriteComment(Commentss: XmlWriter Test Program); 
607InBlock.gif            writer.WriteProcessingInstruction(Instruction,Person Record); 
608InBlock.gif            // Add elements to the file 
609InBlock.gif            writer.WriteStartElement(p, person, urn:person); 
610InBlock.gif            writer.WriteStartElement(LastName,); 
611InBlock.gif            writer.WriteString(Chand); 
612InBlock.gif            writer.WriteEndElement(); 
613InBlock.gif            writer.WriteStartElement(FirstName,); 
614InBlock.gif            writer.WriteString(Mahesh); 
615InBlock.gif            writer.WriteEndElement(); 
616InBlock.gif            writer.WriteElementInt16(age,, 25); 
617InBlock.gif            // Ends the document 
618InBlock.gif            writer.WriteEndDocument(); 
619ExpandedSubBlockEnd.gif        }
 
620InBlock.gif        catch (Exception e) 
621ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{  
622ExpandedSubBlockStart.gifContractedSubBlock.gif            Console.WriteLine (Exception: dot.gif{0}, e.ToString()); 
623ExpandedSubBlockEnd.gif        }
 
624InBlock.gif        return 0
625ExpandedSubBlockEnd.gif    }
 
626ExpandedBlockEnd.gif}
 
627 None.gif
628 None.gifWeb Service方面的:
629 None.gif二十五、一个Web Service的小例子:
630 None.gif <%  @WebService Language = C# Class = TestWS  %>
631 None.gif
632 None.gif using  System.Web.Services;
633 None.gif
634 None.gif public   class  TestWS : System.Web.Services.WebService
635 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
636InBlock.gif    [WebMethod()]
637InBlock.gif    public string StringFromWebService()
638ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
639InBlock.gif        return This is a string from web service.;
640ExpandedSubBlockEnd.gif    }

641ExpandedBlockEnd.gif}
 
642 None.gif

转载于:https://www.cnblogs.com/xA51121/articles/625754.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值