"parameter is incorrect"

Nov 25, 2013 at 6:17 PM
I just keep spinning in circles on this one.
I am able to download files, navigate the directory structure on the ftp server and check if the file I want to upload exists. All without error.
However, now matter how I approach trying to upload (static FtpClient or instance new FtpClient())
I get the same non-descriptive "parameter is incorrect" error upon calling OpenWrite. There is no inner exception.
I realize that this is not a problem with the library since everyone else is able to use it.
I would really appreciate any suggestions you may have though.

Upload snippet:
     conn.Host = "QaFtp";
                conn.Port = 21; //990;
                conn.Credentials = new NetworkCredential("QaFtp|ftptest", "C()mm()n");
                if (conn.IsConnected)
                   using (Stream s = conn.OpenWrite(  "ftp://<IIsFTPServer>/My Dir/MySubDir/test.txt") )
                            // write data to the file on the server
                            const int buffer = 2048;
                            byte[] contentRead = new byte[buffer];
                            int bytesRead;
                            FileInfo fi = new FileInfo("C:\test.text");
                            using (FileStream fs = fi.OpenRead())
                                    bytesRead = fs.Read(contentRead, 0, buffer);
                                    s.Write(contentRead, 0, bytesRead);
                                } while (!(bytesRead < buffer));
Nov 25, 2013 at 7:01 PM
Try changing your loop to:
while((bytesRead = fs.Read(contentRead, 0, buffer)) > 0) {
Nov 25, 2013 at 7:03 PM
do {
  bytesRead = fs.Read(contentRead, 0, buffer);
  if(bytesRead > 0) {
} while(bytresRead > 0);
Return Value
Type: System.Int32
The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.
Nov 25, 2013 at 7:06 PM
Edited Nov 25, 2013 at 7:07 PM
I don't really understand what you're doing in the condition of your loop. What happens if the file is larger than the allocated buffer? Your loop loops as long as you read in less than the buffer size, or at least that's what it looks like. It doesn't make much sense to me.

Also, the OpenWrite() method doesn't except a URI which is probably the real source the problem. It accepts a UNIX style absolute or relative path. The static method for opening a file for upload accepts URI objects.
conn.OpenWrite("/My Dir/MySubDir/test.txt");
Nov 25, 2013 at 10:38 PM
The sample was freehand written for this post and should have been validated before being posted. That said, I have since resolved the issue. The IIS ftp server needed to be restarted. It was running, but returning that cryptic error on OpenWrite calls. After restarting the issue was no longer there.
Thank you for your extremely prompt response and my apologies for the distraction.
Nov 26, 2013 at 8:17 PM
Sorry, just got back from a road trip this morning. No problem and glad it's worked out.