简体   繁体   English

C#文件发送,流问题

[英]C# file send, stream problem

I have a server and a client. 我有一个服务器和一个客户端。 Server sends an executable and an input.txt to the client. 服务器将可执行文件和input.txt发送到客户端。 Client should execute it and send output to the server but I have a problem. 客户端应该执行它,并将输出发送到服务器,但是我遇到了问题。 When I try to run executable it gives an error about argument format. 当我尝试运行可执行文件时,它给出了有关参数格式的错误。 After that I save input file as (make just a quick char addition and removing) executable runs succesfully after saving it as a different file altough it has the exact content. 之后,我将输入文件另存为(只需快速添加和删除字符),然后将可执行文件保存为其他文件(尽管它具有确切的内容)即可成功运行。

I'm saving the file using BinaryWriter : 我使用BinaryWriter保存文件:

FileStream fs = File.Open(filename, FileMode.OpenOrCreate);
BinaryWriter BW = new BinaryWriter(fs);
.......
fs.Close();
BW.Close();

I run executable with the parameter input.txt after closing the BinaryWriter and filestream.I think there is a problem with saving the file or maybe closing the stream but I couldnot find it yet. 关闭BinaryWriter和文件流后,我使用参数input.txt运行可执行文件。我认为保存文件或关闭流有问题,但我找不到它。 Any help would be appreciated... 任何帮助,将不胜感激...

A possible problem is that the last 2 lines are in the wrong order: 一个可能的问题是最后两行的顺序错误:

fs.Close();
BW.Close(); // tries to close the file and maybe flush some buffers

You should at least reverse them, but even better use using blocks: 您至少应该反转它们,但是最好使用block using

using (FileStream fs = File.Open(filename, FileMode.OpenOrCreate))
using (BinaryWriter BW = new BinaryWriter(fs))
{
    .......
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM