C# 文件发送、流问题

发布于 2024-10-10 10:28:43 字数 444 浏览 0 评论 0原文

我有一个服务器和一个客户端。服务器向客户端发送可执行文件和 input.txt。客户端应该执行它并将输出发送到服务器,但我有一个问题。当我尝试运行可执行文件时,它给出了有关参数格式的错误。之后,我将输入文件保存为(仅进行快速字符添加和删除)可执行文件,将其保存为不同的文件后成功运行,尽管它具有确切的内容。

我正在使用 BinaryWriter 保存文件:

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

在关闭 BinaryWriter 和文件流后,我使用参数 input.txt 运行可执行文件。我认为保存文件或关闭流时存在问题,但我还找不到它。任何帮助将不胜感激...

I have a server and a client. Server sends an executable and an input.txt to the client. 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 :

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. Any help would be appreciated...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2024-10-17 10:28:43

一个可能的问题是最后两行的顺序错误:

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

您至少应该颠倒它们,但最好使用 using 块:

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

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:

using (FileStream fs = File.Open(filename, FileMode.OpenOrCreate))
using (BinaryWriter BW = new BinaryWriter(fs))
{
    .......
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文