1、文件的复制
static void Main(string[] args) { FileStream filer = new FileStream(@"dotnet基础加强练习.txt", FileMode.Open, FileAccess.Read); FileStream filew = new FileStream(@"2.txt", FileMode.Create, FileAccess.Write); byte[] by =new byte[100]; int count = 0; using (filer) { using (filew) { while ((count = filer.Read(by, 0, by.Length)) != 0) { filew.Write(by, 0, count); } } } }
2、正则表达式_检索(原文件见txt)
static void Main(string[] args) { List<string> list = new List<string>(); string name = File.ReadAllText(@"百家姓.txt", Encoding.Default); MatchCollection ms = Regex.Matches(name, @"\s*〔(\w{1,2})〕\s*"); foreach (Match m in ms) { if (m.Success) { list.Add(m.Groups[1].Value); Console.WriteLine(m.Groups[1].Value); } } Console.ReadKey(); }
3、小说阅读器
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace 阅读器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string path = Path.GetFullPath("txt"); Func(tv1.Nodes.Add("福尔摩斯全集"),path); } public void Func(TreeNode tn, string path) { string[] dirs = Directory.GetDirectories(path); string[] files = Directory.GetFiles(path, "*.txt"); for (int i = 0; i < dirs.Length; i++) { Func(tn.Nodes.Add(Path.GetFileName(dirs[i])), dirs[i]); } for (int i = 0; i < files.Length; i++) { tn.Nodes.Add(Path.GetFileName(files[i])).Tag = files[i]; } } private void tv1_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Tag != null) { tb1.Text = File.ReadAllText(e.Node.Tag.ToString(),Encoding.Default); } } } }
4、面向对象计算器
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 计算器 7 { 8 class Factory 9 { 10 public static Operation jisuan(string oper, double num1, double num2) 11 { 12 switch (oper) 13 { 14 case "+": return new Add(num1,num2); 15 case "-": return new Sub(num1, num2); 16 case "*": return new Mul(num1, num2); 17 case "/": return new Div(num1, num2); 18 default: return null; 19 20 } 21 } 22 } 23 } 24 using System; 25 using System.Collections.Generic; 26 using System.Linq; 27 using System.Text; 28 29 namespace 计算器 30 { 31 abstract class Operation 32 { 33 public abstract double operate(); 34 double num1; 35 36 public double Num1 37 { 38 get { return num1; } 39 set { num1 = value; } 40 } 41 double num2; 42 43 public double Num2 44 { 45 get { return num2; } 46 set { num2 = value; } 47 } 48 } 49 class Add : Operation 50 { 51 public Add(double num1, double num2) 52 { 53 this.Num1 = num1; 54 this.Num2 = num2; 55 } 56 public override double operate() 57 { 58 return Num1 + Num2; 59 } 60 } 61 class Sub : Operation 62 { 63 public Sub(double num1, double num2) 64 { 65 this.Num1 = num1; 66 this.Num2 = num2; 67 } 68 public override double operate() 69 { 70 return Num1 - Num2; 71 } 72 } 73 class Mul : Operation 74 { 75 public Mul(double num1, double num2) 76 { 77 this.Num1 = num1; 78 this.Num2 = num2; 79 } 80 public override double operate() 81 { 82 return Num1 * Num2; 83 } 84 } 85 class Div : Operation 86 { 87 public Div(double num1, double num2) 88 { 89 this.Num1 = num1; 90 this.Num2 = num2; 91 } 92 public override double operate() 93 { 94 return Num1 / Num2; 95 } 96 } 97 } 98 99 using System; 100 using System.Collections.Generic; 101 using System.Linq; 102 using System.Text; 103 104 namespace 计算器 105 { 106 class Program 107 { 108 static void Main(string[] args) 109 { 110 Console.WriteLine("输入一个数:"); 111 int num1 = Convert.ToInt32(Console.ReadLine()); 112 Console.WriteLine("输入一个数:"); 113 int num2 = Convert.ToInt32(Console.ReadLine()); 114 Console.WriteLine("输入一个数:"); 115 string oper = Console.ReadLine(); 116 Operation myoprate = Factory.jisuan(oper, num1, num2); 117 double res = 0; 118 if (myoprate != null) 119 { 120 res = myoprate.operate(); 121 } 122 123 int num = 0; 124 Console.WriteLine("{0}{1}{2}={3}", num1, oper, num2, num); 125 Console.ReadKey(); 126 } 127 } 128 } 129