我有点问题。在下面的代码中,我从Networkstream接收文件和文件的结尾。之后,文件将保存在我收到的路径下。每次我运行代码时都会得到path.getinvalidpathchars
异常。这是一个简短的代码示例:
ns.Read(ending,0,1212);
string endung = "saved." + Encoding.UTF8.GetString(ending);
string path = Path.Combine(@"c:\users\user\desktop\" , endung);
Console.WriteLine(path);
File.WriteAllBytes(path ,file);
字节数组文件是我收到的文件中的字节数组。完整路径是c:\users\user\desktop\saved.
,其余的(txt,jpg,exe)应该是changebel(这意味着如果收到的字符串sas" txt"路径应该是c:\users\user\desktop\saved.txt
如果它是sas " exe" c:\users\user\desktop\saved.exe
等等!)
答案 0 :(得分:0)
我用StreamReader解决了这个问题!
//byte[] file = File.ReadAllBytes(@"c:\users\user\desktop\file.txt");
TcpListener listen = new TcpListener(IPAddress.Parse(mes),223);
listen.Start();
TcpClient client = listen.AcceptTcpClient();
NetworkStream ns = client.GetStream();
StreamReader sre = new StreamReader(ns);
string ende = sre.ReadLine();
string path = Path.Combine(@"c:\users\user\desktop\file2." + ende);
Console.WriteLine(path);
File.WriteAllBytes(path ,file);
listen.Stop();