UDP|: IPEndPoint A = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001); IPEndPoint B = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8002); UdpClient client1 = new UdpClient(A); UdpClient client2 = new UdpClient(B); string s = "123"; Console.WriteLine("A发送:" + s); byte[] bytes = Encoding.UTF8.GetBytes(s); client1.Send(bytes, bytes.Length, B); byte[] bytes1 = client2.Receive(ref A); string str = Encoding.UTF8.GetString(bytes1); Console.WriteLine("B接收到:" + str); Console.ReadLine(); TCPClient: Console.WriteLine("Client正在连接"); TcpClient client = new TcpClient("127.0.0.1",51888); Console.WriteLine("连接成功"); Console.WriteLine("请输入发送信息:"); string s = Console.ReadLine(); NetworkStream networkstream = client.GetStream(); BinaryReader br = new BinaryReader(networkstream); BinaryWriter bw = new BinaryWriter(networkstream); bw.Write(s); Console.ReadLine(); TCPServer: Console.WriteLine("正在监听"); TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"),51888); listener.Start(); Console.WriteLine("监听成功"); TcpClient client = listener.AcceptTcpClient(); NetworkStream network = client.GetStream(); BinaryReader br = new BinaryReader(network); BinaryWriter bw = new BinaryWriter(network); string s = br.ReadString(); Console.WriteLine("接收消息" + s); Console.ReadKey(); FlieStream: string path=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory+"file1.txt"); while (true) { Console.WriteLine("请选择选项"); Console.WriteLine("1.读文件"); Console.WriteLine("2.写文件"); string choice = Console.ReadLine(); switch (choice) { case "1": { if (!File.Exists(path)) { File.WriteAllText(path, "孙楠\n", Encoding.UTF8); break; } else { string message1; using (FileStream fs = File.OpenRead(path)) { StreamReader sr = new StreamReader(fs); message1= sr.ReadToEnd(); Console.WriteLine(message1); sr.Close(); } } } break; case "2": if (!File.Exists(path)) { File.WriteAllText(path, "孙楠\n", Encoding.UTF8); } else { Console.WriteLine("请输入写入的内容:"); string message = Console.ReadLine(); using (FileStream fs = File.OpenWrite(path)) { StreamWriter sw = new StreamWriter(fs); sw.WriteLine(message); sw.Close(); } } break; default: break; } } Console.ReadLine(); ProcessByName: Console.WriteLine("请输入启动进程的名称(不需要后缀名):"); string filename = Console.ReadLine(); Process p = new Process(); p.StartInfo.FileName = filename; p.StartInfo.WindowStyle = ProcessWindowStyle.Normal; p.Start(); Console.WriteLine("进程已启动"); Console.WriteLine("进程Id为:"+p.Id); Console.WriteLine("进程名称为:" + p.ProcessName); Console.WriteLine("进程启动时间为:" + p.StartTime.ToString("yyyy-M-d HH:mm:ss")); Console.WriteLine("进程内存为:" +string.Format( "{0,10:0}KB",p.WorkingSet64/1024d)); Console.WriteLine("进程路径:"+p.MainModule.FileName); Console.ReadLine(); GetAllProcess: Process[] ps = Process.GetProcesses(); int i = 0; foreach (var p in ps) { i++; try { Console.WriteLine("第" + i + "进程为:" + p.ProcessName); Console.WriteLine("进程Id为:" + p.Id); Console.WriteLine("进程启动时间为:" + p.StartTime.ToString("yyyy-M-d HH:mm:ss")); Console.WriteLine("进程内存为:" + string.Format("{0,10:0}KB", p.WorkingSet64 / 1024d)); Console.WriteLine("进程路径:" + p.MainModule.FileName); } catch { Console.WriteLine("进程路径无法访问"); } } Console.ReadLine(); Thread(WPF): using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading; namespace Thread2 { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public volatile bool IsStop; private void startbtn_Click(object sender, RoutedEventArgs e) { startbtn.IsEnabled = false; stopbtn.IsEnabled = true; IsStop = false; TextBlock1.Text = ""; Thread thread1 = new Thread(MyMethod); thread1.IsBackground = true; thread1.Start("a "); Thread thread2 = new Thread(MyMethod); thread2.IsBackground = true; thread2.Start("b "); Thread thread3 = new Thread(MyMethod); thread3.IsBackground = true; thread3.Start("c "); Thread thread4 = new Thread(MyMethod); thread4.IsBackground = true; thread4.Start("d "); } private void AddMessage(string s) { Action action = delegate() { TextBlock1.Text += s; }; TextBlock1.Dispatcher.Invoke(action); } public void MyMethod(object obj) { string s = obj.ToString(); while (IsStop == false) { AddMessage(s); Thread.Sleep(100); } AddMessage("\n线程已终止\n"); } private void stopbtn_Click(object sender, RoutedEventArgs e) { startbtn.IsEnabled = true; stopbtn.IsEnabled = false; IsStop = true; } } }