【问题标题】:C# file send, stream problemC#文件发送、流问题
【发布时间】:2011-01-04 14:50:35
【问题描述】:

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

我正在使用 BinaryWriter 保存文件:

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

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

【问题讨论】:

  • 这里很难理解客户端、服务器、可执行文件和 input.txt 的角色/功能。
  • @Aaron 错误是“System.FormatException:输入字符串格式不正确”。输入格式为“input.txt output.txt”。使用命令启动我的可执行文件:x.exe input.txt output.txt 但如果不将 input.txt 保存为,它将无法工作。

标签: c# network-programming stream filestream binarywriter


【解决方案1】:

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

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))
{
    .......
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-10
    • 2012-12-08
    • 2013-04-05
    • 2011-06-21
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多