0

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...

aykut
  • 563
  • 2
  • 6
  • 18
  • It is pretty hard to understand what the roles/functions of client, server, executable and input.txt are here. – H H Jan 04 '11 at 15:15
  • @Aaron error is "System.FormatException: Input String was not in a correct format". Input format is "input.txt output.txt". Starting my executable with command : x.exe input.txt output.txt but it does not work without saving input.txt as. – aykut Jan 04 '11 at 16:45

1 Answers1

3

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))
{
    .......
}
H H
  • 263,252
  • 30
  • 330
  • 514