转一下:)
1
2
一、从控制台读取东西代码片断:
3
using System;
4
5
class 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
二、读文件代码片断:
15
using System;
16
using System.IO;
17
18
public 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
三、写文件代码:
38
using System;
39
using System.IO;
40
41
public 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
四、拷贝文件:
56
using System;
57
using System.IO;
58
59
class TestCopyFile
60

{
61
public static void Main()
62
{
63
File.Copy(c:\\temp\\source.txt, C:\\temp\\dest.txt );
64
}
65
}
66
五、移动文件:
67
using System;
68
using System.IO;
69
70
class TestMoveFile
71

{
72
public static void Main()
73
{
74
File.Move(c:\\temp\\abc.txt, C:\\temp\\def.txt );
75
}
76
}
77
六、使用计时器:
78
using System;
79
using System.Timers;
80
81
class 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
七、调用外部程序:
102
class Test
103

{
104
static void Main(string[] args)
105
{
106
System.Diagnostics.Process.Start(notepad.exe);
107
}
108
}
109
110
ADO.NET方面的:
111
八、连接Access数据库:
112
using System;
113
using System.Data;
114
using System.Data.OleDb;
115
116
class 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数据库:
146
using System;
147
using System.Data.SqlClient;
148
149
public 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:
175
using System;
176
using System.Data;
177
using System.Xml;
178
using System.Data.SqlClient;
179
using System.IO;
180
181
public 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添加数据到数据库中:
212
using System;
213
using System.Data;
214
using System.Data.OleDb;
215
216
class 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连接数据库:
242
using System;
243
using System.Data;
244
using System.Data.OleDb;
245
246
class 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
十三、读取表的属性:
269
using System;
270
using System.Data;
271
using System.Data.OleDb;
272
273
class 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
298
ASP.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
328
WinForm开发:
329
十五、一个简单的WinForm程序:
330
using System;
331
using System.Drawing;
332
using System.Collections;
333
using System.ComponentModel;
334
using System.Windows.Forms;
335
using System.Data;
336
337
338
public 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#region Windows Form Designer generated code
362
private void InitializeComponent()
363
{
364
365
this.components = new System.ComponentModel.Container();
366
this.Size = new System.Drawing.Size(300,300);
367
this.Text = Form1;
368
369
this.button1 = new System.Windows.Forms.Button();
370
this.textBox1 = new System.Windows.Forms.TextBox();
371
this.SuspendLayout();
372
//
373
// button1
374
//
375
376
this.button1.Location = new System.Drawing.Point(8, 16);
377
this.button1.Name = button1;
378
this.button1.Size = new System.Drawing.Size(80, 24);
379
this.button1.TabIndex = 0;
380
this.button1.Text = button1;
381
382
//
383
// textBox1
384
//
385
this.textBox1.Location = new System.Drawing.Point(112, 16);
386
this.textBox1.Name = textBox1;
387
this.textBox1.Size = new System.Drawing.Size(160, 20);
388
this.textBox1.TabIndex = 1;
389
this.textBox1.Text = textBox1;
390
//
391
// Form1
392
//
393
394
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
395
this.ClientSize = new System.Drawing.Size(292, 273);
396
this.Controls.AddRange(new System.Windows.Forms.Control[]
{
397
this.textBox1,
398
this.button1});
399
this.Name = Form1;
400
this.Text = Form1;
401
this.ResumeLayout(false);
402
403
}
404
#endregion
405
406
[STAThread]
407
static void Main()
408
{
409
Application.Run(new SimpleForm());
410
}
411
}
412
十六、运行时显示自己定义的图标:
413
//load icon and set to form
414
System.Drawing.Icon ico = new System.Drawing.Icon(@c:\temp\app.ico);
415
this.Icon = ico;
416
十七、添加组件到ListBox中:
417
private 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地址:
430
using System;
431
using System.Net;
432
433
class 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
十九、取得机器名称:
446
using System;
447
using System.Net;
448
449
class GetIP
450

{
451
public static void Main()
452
{
453
Console.WriteLine (Host name :
{0}, Dns.GetHostName());
454
}
455
}
456
二十、发送邮件:
457
using System;
458
using System.Web;
459
using System.Web.Mail;
460
461
public 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地址得出机器名称:
492
using System;
493
using System.Net;
494
495
class 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
504
GDI+方面的:
505
二十二、GDI+入门介绍:
506
using System;
507
using System.Drawing;
508
using System.Collections;
509
using System.ComponentModel;
510
using System.Windows.Forms;
511
using System.Data;
512
513
public 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#region Windows Form Designer generated code
535
private void InitializeComponent()
536
{
537
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
538
this.ClientSize = new System.Drawing.Size(292, 273);
539
this.Name = Form1;
540
this.Text = Form1;
541
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
542
}
543
#endregion
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
560
XML方面的:
561
二十三、读取XML文件:
562
using System;
563
using System.Xml;
564
565
class 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文件:
592
using System;
593
using System.Xml;
594
595
public 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
628
Web Service方面的:
629
二十五、一个Web Service的小例子:
630
<% @WebService Language=C# Class=TestWS %>
631
632
using System.Web.Services;
633
634
public 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

3

4

5

6



7

8



9

10

11

12

13

14

15

16

17

18

19



20

21



22

23

24

25

26

27

28



29

30

31

32

33

34

35

36

37

38

39

40

41

42



43

44



45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60



61

62



63

64

65

66

67

68

69

70

71



72

73



74

75

76

77

78

79

80

81

82



83

84



85

86

87

88

89

90

91

92



93

94

95

96

97



98



99

100

101

102

103



104

105



106

107

108

109

110

111

112

113

114

115

116

117



118

119



120

121

122

123

124

125

126

127



128

129

130

131



132




133

134

135

136



137

138

139

140



141

142

143

144

145

146

147

148

149

150



151

152



153

154

155

156



157

158

159

160

161



162




163

164

165

166

167

168

169



170



171

172

173

174

175

176

177

178

179

180

181

182



183

184



185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217



218

219



220

221

222

223

224

225

226

227



228

229

230

231

232



233



234

235

236



237

238

239

240

241

242

243

244

245

246

247



248

249



250

251

252

253

254

255

256

257

258

259

260

261

262



263

264

265

266

267

268

269

270

271

272

273

274



275

276



277

278

279

280

281

282

283

284

285

286

287

288

289

290

291



292

293

294

295

296

297

298

299

300

301

302

303

304



305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339



340

341

342

343

344

345



346

347

348

349

350



351

352



353

354



355

356

357

358

359

360

361


362

363



364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396



397

398

399

400

401

402

403

404

405

406

407

408



409

410

411

412

413

414

415

416

417

418



419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434



435

436



437

438

439

440



441




442

443

444

445

446

447

448

449

450



451

452



453



454

455

456

457

458

459

460

461

462



463

464



465

466



467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485



486

487

488

489

490

491

492

493

494

495

496



497

498



499

500



501

502

503

504

505

506

507

508

509

510

511

512

513

514



515

516

517

518



519

520

521

522

523



524

525



526

527



528

529

530

531

532

533

534


535

536



537

538

539

540

541

542

543

544

545

546

547



548

549

550

551

552



553

554

555

556

557

558

559

560

561

562

563

564

565

566



567

568



569

570

571

572

573

574



575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596



597

598



599

600



601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621



622



623

624

625

626

627

628

629

630

631

632

633

634

635



636

637

638



639

640

641

642
