转一下:)
1
2一、从控制台读取东西代码片断:
3using System;
4
5class TestReadConsole
6{
7 public static void Main()
8 {
9 Console.Write(Enter your name:);
10 string strName = Console.ReadLine();
11 Console.WriteLine( Hi + strName);
12 }
13}
14二、读文件代码片断:
15using System;
16using System.IO;
17
18public class TestReadFile
19{
20 public static void Main(String[] args)
21 {
22 // Read text file C:\temp\test.txt
23 FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.Open, FileAccess.Read);
24 StreamReader sr = new StreamReader(fs);
25
26 String line=sr.ReadLine();
27 while (line!=null)
28 {
29 Console.WriteLine(line);
30 line=sr.ReadLine();
31 }
32
33 sr.Close();
34 fs.Close();
35 }
36}
37三、写文件代码:
38using System;
39using System.IO;
40
41public class TestWriteFile
42{
43 public static void Main(String[] args)
44 {
45 // Create a text file C:\temp\test.txt
46 FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.OpenOrCreate, FileAccess.Write);
47 StreamWriter sw = new StreamWriter(fs);
48 // Write to the file using StreamWriter class
49 sw.BaseStream.Seek(0, SeekOrigin.End);
50 sw.WriteLine( First Line );
51 sw.WriteLine( Second Line);
52 sw.Flush();
53 }
54}
55四、拷贝文件:
56using System;
57using System.IO;
58
59class TestCopyFile
60{
61 public static void Main()
62 {
63 File.Copy(c:\\temp\\source.txt, C:\\temp\\dest.txt );
64 }
65}
66五、移动文件:
67using System;
68using System.IO;
69
70class TestMoveFile
71{
72 public static void Main()
73 {
74 File.Move(c:\\temp\\abc.txt, C:\\temp\\def.txt );
75 }
76}
77六、使用计时器:
78using System;
79using System.Timers;
80
81class TestTimer
82{
83 public static void Main()
84 {
85 Timer timer = new Timer();
86 timer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
87 timer.Interval = 1000;
88 timer.Start();
89 timer.Enabled = true;
90
91 while ( Console.Read() != 'q' )
92 {
93 //-------------
94 }
95 }
96 public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
97 {
98 Console.Write(\r{0}, DateTime.Now);
99 }
100}
101七、调用外部程序:
102class Test
103{
104 static void Main(string[] args)
105 {
106 System.Diagnostics.Process.Start(notepad.exe);
107 }
108}
109
110ADO.NET方面的:
111八、连接Access数据库:
112using System;
113using System.Data;
114using System.Data.OleDb;
115
116class TestADO
117{
118 static void Main(string[] args)
119 {
120 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.mdb;
121 string strSQL = SELECT * FROM employees ;
122
123 OleDbConnection conn = new OleDbConnection(strDSN);
124 OleDbCommand cmd = new OleDbCommand( strSQL, conn );
125 OleDbDataReader reader = null;
126 try
127 {
128 conn.Open();
129 reader = cmd.ExecuteReader();
130 while (reader.Read() )
131 {
132 Console.WriteLine(First Name:{0}, Last Name:{1}, reader[FirstName], reader[LastName]);
133 }
134 }
135 catch (Exception e)
136 {
137 Console.WriteLine(e.Message);
138 }
139 finally
140 {
141 conn.Close();
142 }
143 }
144}
145九、连接SQL Server数据库:
146using System;
147using System.Data.SqlClient;
148
149public class TestADO
150{
151 public static void Main()
152 {
153 SqlConnection conn = new SqlConnection(Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs);
154 SqlCommand cmd = new SqlCommand(SELECT * FROM employees, conn);
155 try
156 {
157 conn.Open();
158
159 SqlDataReader reader = cmd.ExecuteReader();
160 while (reader.Read())
161 {
162 Console.WriteLine(First Name: {0}, Last Name: {1}, reader.GetString(0), reader.GetString(1));
163 }
164
165 reader.Close();
166 conn.Close();
167 }
168 catch(Exception e)
169 {
170 Console.WriteLine(Exception Occured -->> {0},e);
171 }
172 }
173}
174十、从SQL内读数据到XML:
175using System;
176using System.Data;
177using System.Xml;
178using System.Data.SqlClient;
179using System.IO;
180
181public class TestWriteXML
182{
183 public static void Main()
184 {
185
186 String strFileName=c:/temp/output.xml;
187
188 SqlConnection conn = new SqlConnection(server=localhost;uid=sa;pwd=;database=db);
189
190 String strSql = SELECT FirstName, LastName FROM employees;
191
192 SqlDataAdapter adapter = new SqlDataAdapter();
193
194 adapter.SelectCommand = new SqlCommand(strSql,conn);
195
196 // Build the DataSet
197 DataSet ds = new DataSet();
198
199 adapter.Fill(ds, employees);
200
201 // Get a FileStream object
202 FileStream fs = new FileStream(strFileName,FileMode.OpenOrCreate,FileAccess.Write);
203
204 // Apply the WriteXml method to write an XML document
205 ds.WriteXml(fs);
206
207 fs.Close();
208
209 }
210}
211十一、用ADO添加数据到数据库中:
212using System;
213using System.Data;
214using System.Data.OleDb;
215
216class TestADO
217{
218 static void Main(string[] args)
219 {
220 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;
221 string strSQL = INSERT INTO Employee(FirstName, LastName) VALUES('FirstName', 'LastName') ;
222
223 // create Objects of ADOConnection and ADOCommand
224 OleDbConnection conn = new OleDbConnection(strDSN);
225 OleDbCommand cmd = new OleDbCommand( strSQL, conn );
226 try
227 {
228 conn.Open();
229 cmd.ExecuteNonQuery();
230 }
231 catch (Exception e)
232 {
233 Console.WriteLine(Oooops. I did it again:\n{0}, e.Message);
234 }
235 finally
236 {
237 conn.Close();
238 }
239 }
240}
241十二、使用OLEConn连接数据库:
242using System;
243using System.Data;
244using System.Data.OleDb;
245
246class TestADO
247{
248 static void Main(string[] args)
249 {
250 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;
251 string strSQL = SELECT * FROM employee ;
252
253 OleDbConnection conn = new OleDbConnection(strDSN);
254 OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn );
255
256 conn.Open();
257 DataSet ds = new DataSet();
258 cmd.Fill( ds, employee );
259 DataTable dt = ds.Tables[0];
260
261 foreach( DataRow dr in dt.Rows )
262 {
263 Console.WriteLine(First name: + dr[FirstName].ToString() + Last name: + dr[LastName].ToString());
264 }
265 conn.Close();
266 }
267}
268十三、读取表的属性:
269using System;
270using System.Data;
271using System.Data.OleDb;
272
273class TestADO
274{
275 static void Main(string[] args)
276 {
277 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;
278 string strSQL = SELECT * FROM employee ;
279
280 OleDbConnection conn = new OleDbConnection(strDSN);
281 OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn );
282
283 conn.Open();
284 DataSet ds = new DataSet();
285 cmd.Fill( ds, employee );
286 DataTable dt = ds.Tables[0];
287
288 Console.WriteLine(Field Name DataType Unique AutoIncrement AllowNull);
289 Console.WriteLine(==================================================================);
290 foreach( DataColumn dc in dt.Columns )
291 {
292 Console.WriteLine(dc.ColumnName+ , +dc.DataType + ,+dc.Unique + ,+dc.AutoIncrement+ ,+dc.AllowDBNull );
293 }
294 conn.Close();
295 }
296}
297
298ASP.NET方面的
299十四、一个ASP.NET程序:
300<%@ Page Language=C# %>
301<script runat=server>
302
303 void Button1_Click(Object sender, EventArgs e)
304 {
305 Label1.Text=TextBox1.Text;
306 }
307
308</script>
309<html>
310<head>
311</head>
312<body>
313 <form runat=server>
314 <p>
315 <br />
316 Enter your name: <asp:TextBox id=TextBox1 runat=server></asp:TextBox>
317 </p>
318 <p>
319 <b><asp:Label id=Label1 runat=server Width=247px></asp:Label></b>
320 </p>
321 <p>
322 <asp:Button id=Button1 onclick=Button1_Click runat=server Text=Submit></asp:Button>
323 </p>
324 </form>
325</body>
326</html>
327
328WinForm开发:
329十五、一个简单的WinForm程序:
330using System;
331using System.Drawing;
332using System.Collections;
333using System.ComponentModel;
334using System.Windows.Forms;
335using System.Data;
336
337
338public class SimpleForm : System.Windows.Forms.Form
339{
340
341 private System.ComponentModel.Container components = null;
342 private System.Windows.Forms.Button button1;
343 private System.Windows.Forms.TextBox textBox1;
344 public SimpleForm()
345 {
346 InitializeComponent();
347 }
348
349 protected override void Dispose( bool disposing )
350 {
351 if( disposing )
352 {
353 if (components != null)
354 {
355 components.Dispose();
356 }
357 }
358 base.Dispose( disposing );
359 }
360
361 Windows Form Designer generated code
405
406 [STAThread]
407 static void Main()
408 {
409 Application.Run(new SimpleForm());
410 }
411}
412十六、运行时显示自己定义的图标:
413//load icon and set to form
414System.Drawing.Icon ico = new System.Drawing.Icon(@c:\temp\app.ico);
415this.Icon = ico;
416十七、添加组件到ListBox中:
417private void Form1_Load(object sender, System.EventArgs e)
418{
419 string str = First item;
420 int i = 23;
421 float flt = 34.98f;
422 listBox1.Items.Add(str);
423 listBox1.Items.Add(i.ToString());
424 listBox1.Items.Add(flt.ToString());
425 listBox1.Items.Add(Last Item in the List Box);
426}
427
428网络方面的:
429十八、取得IP地址:
430using System;
431using System.Net;
432
433class GetIP
434{
435 public static void Main()
436 {
437 IPHostEntry ipEntry = Dns.GetHostByName (localhost);
438 IPAddress [] IpAddr = ipEntry.AddressList;
439 for (int i = 0; i < IpAddr.Length; i++)
440 {
441 Console.WriteLine (IP Address {0}: {1} , i, IpAddr.ToString ());
442 }
443 }
444}
445十九、取得机器名称:
446using System;
447using System.Net;
448
449class GetIP
450{
451 public static void Main()
452 {
453 Console.WriteLine (Host name : {0}, Dns.GetHostName());
454 }
455}
456二十、发送邮件:
457using System;
458using System.Web;
459using System.Web.Mail;
460
461public class TestSendMail
462{
463 public static void Main()
464 {
465 try
466 {
467 // Construct a new mail message
468 MailMessage message = new MailMessage();
469 message.From = from@domain.com;
470 message.To = pengyun@cobainsoft.com;
471 message.Cc = ;
472 message.Bcc = ;
473 message.Subject = Subject;
474 message.Body = Content of message;
475
476 //if you want attach file with this mail, add the line below
477 message.Attachments.Add(new MailAttachment(c:\\attach.txt, MailEncoding.Base64));
478
479 // Send the message
480 SmtpMail.Send(message);
481 System.Console.WriteLine(Message has been sent);
482 }
483
484 catch(Exception ex)
485 {
486 System.Console.WriteLine(ex.Message.ToString());
487 }
488
489 }
490}
491二十一、根据IP地址得出机器名称:
492using System;
493using System.Net;
494
495class ResolveIP
496{
497 public static void Main()
498 {
499 IPHostEntry ipEntry = Dns.Resolve(172.29.9.9);
500 Console.WriteLine (Host name : {0}, ipEntry.HostName);
501 }
502}
503
504GDI+方面的:
505二十二、GDI+入门介绍:
506using System;
507using System.Drawing;
508using System.Collections;
509using System.ComponentModel;
510using System.Windows.Forms;
511using System.Data;
512
513public class Form1 : System.Windows.Forms.Form
514{
515 private System.ComponentModel.Container components = null;
516
517 public Form1()
518 {
519 InitializeComponent();
520 }
521
522 protected override void Dispose( bool disposing )
523 {
524 if( disposing )
525 {
526 if (components != null)
527 {
528 components.Dispose();
529 }
530 }
531 base.Dispose( disposing );
532 }
533
534 Windows Form Designer generated code
544
545 [STAThread]
546 static void Main()
547 {
548 Application.Run(new Form1());
549 }
550
551 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
552 {
553 Graphics g=e.Graphics;
554 g.DrawLine(new Pen(Color.Blue),10,10,210,110);
555 g.DrawRectangle(new Pen(Color.Red),10,10,200,100);
556 g.DrawEllipse(new Pen(Color.Yellow),10,150,200,100);
557 }
558}
559
560XML方面的:
561二十三、读取XML文件:
562using System;
563using System.Xml;
564
565class TestReadXML
566{
567 public static void Main()
568 {
569
570 XmlTextReader reader = new XmlTextReader(C:\\test.xml);
571 reader.Read();
572
573 while (reader.Read())
574 {
575 reader.MoveToElement();
576 Console.WriteLine(XmlTextReader Properties Test);
577 Console.WriteLine(===================);
578
579 // Read this properties of element and display them on console
580 Console.WriteLine(Name: + reader.Name);
581 Console.WriteLine(Base URI: + reader.BaseURI);
582 Console.WriteLine(Local Name: + reader.LocalName);
583 Console.WriteLine(Attribute Count: + reader.AttributeCount.ToString());
584 Console.WriteLine(Depth: + reader.Depth.ToString());
585 Console.WriteLine(Line Number: + reader.LineNumber.ToString());
586 Console.WriteLine(Node Type: + reader.NodeType.ToString());
587 Console.WriteLine(Attribute Count: + reader.Value.ToString());
588 }
589 }
590}
591二十四、写XML文件:
592using System;
593using System.Xml;
594
595public class TestWriteXMLFile
596{
597 public static int Main(string[] args)
598 {
599 try
600 {
601 // Creates an XML file is not exist
602 XmlTextWriter writer = new XmlTextWriter(C:\\temp\\xmltest.xml, null);
603 // Starts a new document
604 writer.WriteStartDocument();
605 //Write comments
606 writer.WriteComment(Commentss: XmlWriter Test Program);
607 writer.WriteProcessingInstruction(Instruction,Person Record);
608 // Add elements to the file
609 writer.WriteStartElement(p, person, urn:person);
610 writer.WriteStartElement(LastName,);
611 writer.WriteString(Chand);
612 writer.WriteEndElement();
613 writer.WriteStartElement(FirstName,);
614 writer.WriteString(Mahesh);
615 writer.WriteEndElement();
616 writer.WriteElementInt16(age,, 25);
617 // Ends the document
618 writer.WriteEndDocument();
619 }
620 catch (Exception e)
621 {
622 Console.WriteLine (Exception: {0}, e.ToString());
623 }
624 return 0;
625 }
626}
627
628Web Service方面的:
629二十五、一个Web Service的小例子:
630<% @WebService Language=C# Class=TestWS %>
631
632using System.Web.Services;
633
634public class TestWS : System.Web.Services.WebService
635{
636 [WebMethod()]
637 public string StringFromWebService()
638 {
639 return This is a string from web service.;
640 }
641}
642
2一、从控制台读取东西代码片断:
3using System;
4
5class TestReadConsole
6{
7 public static void Main()
8 {
9 Console.Write(Enter your name:);
10 string strName = Console.ReadLine();
11 Console.WriteLine( Hi + strName);
12 }
13}
14二、读文件代码片断:
15using System;
16using System.IO;
17
18public class TestReadFile
19{
20 public static void Main(String[] args)
21 {
22 // Read text file C:\temp\test.txt
23 FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.Open, FileAccess.Read);
24 StreamReader sr = new StreamReader(fs);
25
26 String line=sr.ReadLine();
27 while (line!=null)
28 {
29 Console.WriteLine(line);
30 line=sr.ReadLine();
31 }
32
33 sr.Close();
34 fs.Close();
35 }
36}
37三、写文件代码:
38using System;
39using System.IO;
40
41public class TestWriteFile
42{
43 public static void Main(String[] args)
44 {
45 // Create a text file C:\temp\test.txt
46 FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.OpenOrCreate, FileAccess.Write);
47 StreamWriter sw = new StreamWriter(fs);
48 // Write to the file using StreamWriter class
49 sw.BaseStream.Seek(0, SeekOrigin.End);
50 sw.WriteLine( First Line );
51 sw.WriteLine( Second Line);
52 sw.Flush();
53 }
54}
55四、拷贝文件:
56using System;
57using System.IO;
58
59class TestCopyFile
60{
61 public static void Main()
62 {
63 File.Copy(c:\\temp\\source.txt, C:\\temp\\dest.txt );
64 }
65}
66五、移动文件:
67using System;
68using System.IO;
69
70class TestMoveFile
71{
72 public static void Main()
73 {
74 File.Move(c:\\temp\\abc.txt, C:\\temp\\def.txt );
75 }
76}
77六、使用计时器:
78using System;
79using System.Timers;
80
81class TestTimer
82{
83 public static void Main()
84 {
85 Timer timer = new Timer();
86 timer.Elapsed += new ElapsedEventHandler( DisplayTimeEvent );
87 timer.Interval = 1000;
88 timer.Start();
89 timer.Enabled = true;
90
91 while ( Console.Read() != 'q' )
92 {
93 //-------------
94 }
95 }
96 public static void DisplayTimeEvent( object source, ElapsedEventArgs e )
97 {
98 Console.Write(\r{0}, DateTime.Now);
99 }
100}
101七、调用外部程序:
102class Test
103{
104 static void Main(string[] args)
105 {
106 System.Diagnostics.Process.Start(notepad.exe);
107 }
108}
109
110ADO.NET方面的:
111八、连接Access数据库:
112using System;
113using System.Data;
114using System.Data.OleDb;
115
116class TestADO
117{
118 static void Main(string[] args)
119 {
120 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\test.mdb;
121 string strSQL = SELECT * FROM employees ;
122
123 OleDbConnection conn = new OleDbConnection(strDSN);
124 OleDbCommand cmd = new OleDbCommand( strSQL, conn );
125 OleDbDataReader reader = null;
126 try
127 {
128 conn.Open();
129 reader = cmd.ExecuteReader();
130 while (reader.Read() )
131 {
132 Console.WriteLine(First Name:{0}, Last Name:{1}, reader[FirstName], reader[LastName]);
133 }
134 }
135 catch (Exception e)
136 {
137 Console.WriteLine(e.Message);
138 }
139 finally
140 {
141 conn.Close();
142 }
143 }
144}
145九、连接SQL Server数据库:
146using System;
147using System.Data.SqlClient;
148
149public class TestADO
150{
151 public static void Main()
152 {
153 SqlConnection conn = new SqlConnection(Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs);
154 SqlCommand cmd = new SqlCommand(SELECT * FROM employees, conn);
155 try
156 {
157 conn.Open();
158
159 SqlDataReader reader = cmd.ExecuteReader();
160 while (reader.Read())
161 {
162 Console.WriteLine(First Name: {0}, Last Name: {1}, reader.GetString(0), reader.GetString(1));
163 }
164
165 reader.Close();
166 conn.Close();
167 }
168 catch(Exception e)
169 {
170 Console.WriteLine(Exception Occured -->> {0},e);
171 }
172 }
173}
174十、从SQL内读数据到XML:
175using System;
176using System.Data;
177using System.Xml;
178using System.Data.SqlClient;
179using System.IO;
180
181public class TestWriteXML
182{
183 public static void Main()
184 {
185
186 String strFileName=c:/temp/output.xml;
187
188 SqlConnection conn = new SqlConnection(server=localhost;uid=sa;pwd=;database=db);
189
190 String strSql = SELECT FirstName, LastName FROM employees;
191
192 SqlDataAdapter adapter = new SqlDataAdapter();
193
194 adapter.SelectCommand = new SqlCommand(strSql,conn);
195
196 // Build the DataSet
197 DataSet ds = new DataSet();
198
199 adapter.Fill(ds, employees);
200
201 // Get a FileStream object
202 FileStream fs = new FileStream(strFileName,FileMode.OpenOrCreate,FileAccess.Write);
203
204 // Apply the WriteXml method to write an XML document
205 ds.WriteXml(fs);
206
207 fs.Close();
208
209 }
210}
211十一、用ADO添加数据到数据库中:
212using System;
213using System.Data;
214using System.Data.OleDb;
215
216class TestADO
217{
218 static void Main(string[] args)
219 {
220 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;
221 string strSQL = INSERT INTO Employee(FirstName, LastName) VALUES('FirstName', 'LastName') ;
222
223 // create Objects of ADOConnection and ADOCommand
224 OleDbConnection conn = new OleDbConnection(strDSN);
225 OleDbCommand cmd = new OleDbCommand( strSQL, conn );
226 try
227 {
228 conn.Open();
229 cmd.ExecuteNonQuery();
230 }
231 catch (Exception e)
232 {
233 Console.WriteLine(Oooops. I did it again:\n{0}, e.Message);
234 }
235 finally
236 {
237 conn.Close();
238 }
239 }
240}
241十二、使用OLEConn连接数据库:
242using System;
243using System.Data;
244using System.Data.OleDb;
245
246class TestADO
247{
248 static void Main(string[] args)
249 {
250 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;
251 string strSQL = SELECT * FROM employee ;
252
253 OleDbConnection conn = new OleDbConnection(strDSN);
254 OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn );
255
256 conn.Open();
257 DataSet ds = new DataSet();
258 cmd.Fill( ds, employee );
259 DataTable dt = ds.Tables[0];
260
261 foreach( DataRow dr in dt.Rows )
262 {
263 Console.WriteLine(First name: + dr[FirstName].ToString() + Last name: + dr[LastName].ToString());
264 }
265 conn.Close();
266 }
267}
268十三、读取表的属性:
269using System;
270using System.Data;
271using System.Data.OleDb;
272
273class TestADO
274{
275 static void Main(string[] args)
276 {
277 string strDSN = Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.mdb;
278 string strSQL = SELECT * FROM employee ;
279
280 OleDbConnection conn = new OleDbConnection(strDSN);
281 OleDbDataAdapter cmd = new OleDbDataAdapter( strSQL, conn );
282
283 conn.Open();
284 DataSet ds = new DataSet();
285 cmd.Fill( ds, employee );
286 DataTable dt = ds.Tables[0];
287
288 Console.WriteLine(Field Name DataType Unique AutoIncrement AllowNull);
289 Console.WriteLine(==================================================================);
290 foreach( DataColumn dc in dt.Columns )
291 {
292 Console.WriteLine(dc.ColumnName+ , +dc.DataType + ,+dc.Unique + ,+dc.AutoIncrement+ ,+dc.AllowDBNull );
293 }
294 conn.Close();
295 }
296}
297
298ASP.NET方面的
299十四、一个ASP.NET程序:
300<%@ Page Language=C# %>
301<script runat=server>
302
303 void Button1_Click(Object sender, EventArgs e)
304 {
305 Label1.Text=TextBox1.Text;
306 }
307
308</script>
309<html>
310<head>
311</head>
312<body>
313 <form runat=server>
314 <p>
315 <br />
316 Enter your name: <asp:TextBox id=TextBox1 runat=server></asp:TextBox>
317 </p>
318 <p>
319 <b><asp:Label id=Label1 runat=server Width=247px></asp:Label></b>
320 </p>
321 <p>
322 <asp:Button id=Button1 onclick=Button1_Click runat=server Text=Submit></asp:Button>
323 </p>
324 </form>
325</body>
326</html>
327
328WinForm开发:
329十五、一个简单的WinForm程序:
330using System;
331using System.Drawing;
332using System.Collections;
333using System.ComponentModel;
334using System.Windows.Forms;
335using System.Data;
336
337
338public class SimpleForm : System.Windows.Forms.Form
339{
340
341 private System.ComponentModel.Container components = null;
342 private System.Windows.Forms.Button button1;
343 private System.Windows.Forms.TextBox textBox1;
344 public SimpleForm()
345 {
346 InitializeComponent();
347 }
348
349 protected override void Dispose( bool disposing )
350 {
351 if( disposing )
352 {
353 if (components != null)
354 {
355 components.Dispose();
356 }
357 }
358 base.Dispose( disposing );
359 }
360
361 Windows Form Designer generated code
405
406 [STAThread]
407 static void Main()
408 {
409 Application.Run(new SimpleForm());
410 }
411}
412十六、运行时显示自己定义的图标:
413//load icon and set to form
414System.Drawing.Icon ico = new System.Drawing.Icon(@c:\temp\app.ico);
415this.Icon = ico;
416十七、添加组件到ListBox中:
417private void Form1_Load(object sender, System.EventArgs e)
418{
419 string str = First item;
420 int i = 23;
421 float flt = 34.98f;
422 listBox1.Items.Add(str);
423 listBox1.Items.Add(i.ToString());
424 listBox1.Items.Add(flt.ToString());
425 listBox1.Items.Add(Last Item in the List Box);
426}
427
428网络方面的:
429十八、取得IP地址:
430using System;
431using System.Net;
432
433class GetIP
434{
435 public static void Main()
436 {
437 IPHostEntry ipEntry = Dns.GetHostByName (localhost);
438 IPAddress [] IpAddr = ipEntry.AddressList;
439 for (int i = 0; i < IpAddr.Length; i++)
440 {
441 Console.WriteLine (IP Address {0}: {1} , i, IpAddr.ToString ());
442 }
443 }
444}
445十九、取得机器名称:
446using System;
447using System.Net;
448
449class GetIP
450{
451 public static void Main()
452 {
453 Console.WriteLine (Host name : {0}, Dns.GetHostName());
454 }
455}
456二十、发送邮件:
457using System;
458using System.Web;
459using System.Web.Mail;
460
461public class TestSendMail
462{
463 public static void Main()
464 {
465 try
466 {
467 // Construct a new mail message
468 MailMessage message = new MailMessage();
469 message.From = from@domain.com;
470 message.To = pengyun@cobainsoft.com;
471 message.Cc = ;
472 message.Bcc = ;
473 message.Subject = Subject;
474 message.Body = Content of message;
475
476 //if you want attach file with this mail, add the line below
477 message.Attachments.Add(new MailAttachment(c:\\attach.txt, MailEncoding.Base64));
478
479 // Send the message
480 SmtpMail.Send(message);
481 System.Console.WriteLine(Message has been sent);
482 }
483
484 catch(Exception ex)
485 {
486 System.Console.WriteLine(ex.Message.ToString());
487 }
488
489 }
490}
491二十一、根据IP地址得出机器名称:
492using System;
493using System.Net;
494
495class ResolveIP
496{
497 public static void Main()
498 {
499 IPHostEntry ipEntry = Dns.Resolve(172.29.9.9);
500 Console.WriteLine (Host name : {0}, ipEntry.HostName);
501 }
502}
503
504GDI+方面的:
505二十二、GDI+入门介绍:
506using System;
507using System.Drawing;
508using System.Collections;
509using System.ComponentModel;
510using System.Windows.Forms;
511using System.Data;
512
513public class Form1 : System.Windows.Forms.Form
514{
515 private System.ComponentModel.Container components = null;
516
517 public Form1()
518 {
519 InitializeComponent();
520 }
521
522 protected override void Dispose( bool disposing )
523 {
524 if( disposing )
525 {
526 if (components != null)
527 {
528 components.Dispose();
529 }
530 }
531 base.Dispose( disposing );
532 }
533
534 Windows Form Designer generated code
544
545 [STAThread]
546 static void Main()
547 {
548 Application.Run(new Form1());
549 }
550
551 private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
552 {
553 Graphics g=e.Graphics;
554 g.DrawLine(new Pen(Color.Blue),10,10,210,110);
555 g.DrawRectangle(new Pen(Color.Red),10,10,200,100);
556 g.DrawEllipse(new Pen(Color.Yellow),10,150,200,100);
557 }
558}
559
560XML方面的:
561二十三、读取XML文件:
562using System;
563using System.Xml;
564
565class TestReadXML
566{
567 public static void Main()
568 {
569
570 XmlTextReader reader = new XmlTextReader(C:\\test.xml);
571 reader.Read();
572
573 while (reader.Read())
574 {
575 reader.MoveToElement();
576 Console.WriteLine(XmlTextReader Properties Test);
577 Console.WriteLine(===================);
578
579 // Read this properties of element and display them on console
580 Console.WriteLine(Name: + reader.Name);
581 Console.WriteLine(Base URI: + reader.BaseURI);
582 Console.WriteLine(Local Name: + reader.LocalName);
583 Console.WriteLine(Attribute Count: + reader.AttributeCount.ToString());
584 Console.WriteLine(Depth: + reader.Depth.ToString());
585 Console.WriteLine(Line Number: + reader.LineNumber.ToString());
586 Console.WriteLine(Node Type: + reader.NodeType.ToString());
587 Console.WriteLine(Attribute Count: + reader.Value.ToString());
588 }
589 }
590}
591二十四、写XML文件:
592using System;
593using System.Xml;
594
595public class TestWriteXMLFile
596{
597 public static int Main(string[] args)
598 {
599 try
600 {
601 // Creates an XML file is not exist
602 XmlTextWriter writer = new XmlTextWriter(C:\\temp\\xmltest.xml, null);
603 // Starts a new document
604 writer.WriteStartDocument();
605 //Write comments
606 writer.WriteComment(Commentss: XmlWriter Test Program);
607 writer.WriteProcessingInstruction(Instruction,Person Record);
608 // Add elements to the file
609 writer.WriteStartElement(p, person, urn:person);
610 writer.WriteStartElement(LastName,);
611 writer.WriteString(Chand);
612 writer.WriteEndElement();
613 writer.WriteStartElement(FirstName,);
614 writer.WriteString(Mahesh);
615 writer.WriteEndElement();
616 writer.WriteElementInt16(age,, 25);
617 // Ends the document
618 writer.WriteEndDocument();
619 }
620 catch (Exception e)
621 {
622 Console.WriteLine (Exception: {0}, e.ToString());
623 }
624 return 0;
625 }
626}
627
628Web Service方面的:
629二十五、一个Web Service的小例子:
630<% @WebService Language=C# Class=TestWS %>
631
632using System.Web.Services;
633
634public class TestWS : System.Web.Services.WebService
635{
636 [WebMethod()]
637 public string StringFromWebService()
638 {
639 return This is a string from web service.;
640 }
641}
642