Please Help - GetFileSize returning -1 after OpenWrite and Stream.CopyTo (but then returns the correct size after reconnect)

Mar 28, 2016 at 7:57 PM
Can someone please explain what I can do about this? I have the following method that copies a file from directory to an FTP server using System.Net.FtpClient. I am using OpenWrite() which creates a file and returns a stream object. Then I copy the data into the stream. Then I check the file size to make sure the data was written. The response from the GetFileSize() method is -1 (which must be an error?) When I shut app down and reconnect to the ftp server and call the GetFileSize() it returns the correct size of the file.

How can I be sure the data gets written to the ftp server? I need to know because I could lose data if I assume it was copied and then delete the source file.


Here is my code:
public static void MoveFromDirectoryToFTP()
        {
           using (FtpClient destinationServer = new FtpClient())
            {
                destinationServer.Host = 11.11.11.11;
                destinationServer.Credentials = new NetworkCredential("me", "xx");
                destinationServer.Connect();
                destinationServer.SetWorkingDirectory("/mydatafolder/");

                foreach (var file in Directory.EnumerateFiles(@"c:\mysourcedir\"))
                {
                    try
                    {
                        if (!File.Exists(file))
                        {
                            continue;
                        }

                        using (FileStream sourceStream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                        {
                            if (sourceStream.Length == 0) continue;

                            using (Stream destinationStream = destinationServer.OpenWrite(Path.GetFileName(file), FtpDataType.Binary))
                            {
                                sourceStream.CopyTo(destinationStream, (int)sourceStream.Length);
                            }
                        }
                        Debug.WriteLine("size after copy: " + destinationServer.GetFileSize(Path.GetFileName(file))); //returning -1

                        if (destinationServer.GetFileSize(Path.GetFileName(file)) > 0) //returning -1
                        {
                            File.Delete(file);
                        }
                    }
                    catch (Exception e)
                    {
                        Helper.LogErrorToDisk(e.Message + " when processing file " + file, e.Source, e.StackTrace);
                    }
                }
            }
        }
Coordinator
Mar 29, 2016 at 1:45 AM
Can you provide a trace? Check the examples. Also, checking file size is not a good verification for your data. There are hashing extensions provided in this library if you use a FTP server that supports them. If you don't I suggest you stop using FTP and use something that provides a way to do checksums for verifying your uploads.