问题描述
- C# FileStream读取 结果没有在指定路径生成文件
-
发送文件
FileStream fs = File.Open("f:/屏幕获取保存/2015350211352135.jpeg", FileMode.Open);byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (int)fs.Length); fs.Close(); try { TcpClient client = new TcpClient(); client.Connect("127.0.0.1",4020); NetworkStream ns = client.GetStream(); ns.Write(buffer, 0, buffer.Length);
接收文件
bool done = false;
IPAddress ip = new IPAddress(new byte[] { 127, 0, 0, 1 });
TcpListener listener = new TcpListener(ip, 4020);listener.Start(); // 开始侦听 while (!done) { TcpClient client = listener.AcceptTcpClient(); NetworkStream ns = client.GetStream(); byte[] tempBuffer = new byte[4096]; FileStream fs = File.Open( "d:/4567.jpeg", FileMode.CreateNew); int bytesRead = 0; do { bytesRead = ns.Read(tempBuffer, 0, 4096); fs.Write(tempBuffer, 0, bytesRead); }while (bytesRead > 0); fs.Close(); Console.WriteLine("file accepted."); } listener.Stop(); } 结果在d盘没有看到4567.jpeg
解决方案
用而不是/作为路径分割
解决方案二:
C#获取指定路径的所有文件
C# 打开指定路径下文件
C#读取文件路径
解决方案三:
c#文件的读写
时间: 2025-01-05 14:57:54