0

The purpose of my application is, it has to uploade files to a FTP server, and then move the local files to an Archive folder. Here is my code:

public void UploadLocalFiles(string folderName)
        {
            try
            {

                string localPath = @"\\Mobileconnect\filedrop_to_ssis\" + folderName;
                string[] files = Directory.GetFiles(localPath);

                foreach (string filepath in files)
                {
                    string fileName = Path.GetFileName(filepath);
                    localFileNames = files;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp:...../inbox/" + fileName));
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                    reqFTP.UsePassive = true;
                    reqFTP.UseBinary = true;
                    reqFTP.ServicePoint.ConnectionLimit = files.Length;
                    reqFTP.Credentials = new NetworkCredential("username", "password");
                    reqFTP.EnableSsl = true;
                    ServicePointManager.ServerCertificateValidationCallback = Certificate;

                    FileInfo fileInfo = new FileInfo(localPath + @"\" + fileName);
                    byte[] fileContents = new byte[fileInfo.Length];

                    FileStream fileStream = fileInfo.OpenRead();

                    fileStream.Read(fileContents, 0, Convert.ToInt32(fileInfo.Length));


                    Stream writer = reqFTP.GetRequestStream();

                    writer.Write(fileContents, 0, fileContents.Length);
                }

                reqFTP.Abort();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error in GetLocalFileList method!!!!!" + e.Message);
            }

        }

After runing this method, i cant move the files, i get this exception message : "Cant access file, the file is being used by another process". It is my Filestream or Stream that is locking my files. When i surround Filestream and Stream by using it doesn't uploade the files to the FTP as it does wihtout using. I cant see why, can anyone help with this ?

Lahib
  • 1,305
  • 5
  • 34
  • 62

2 Answers2

2

The problem is in filestream, that you are using to read files.

You need to close it.

Just add fileStream.Close() just before end of foreach loop.

  • Thank you for the tip. It worked finally. I thought it was my Stream that was locking the file. But ofcourse it was the Filestream because it has access to the local file. – Lahib Sep 26 '12 at 11:00
1

Try using FileStream.Dispose after you're done. This should have the same effect as 'using'.

Amr
  • 1,935
  • 2
  • 19
  • 29
  • i have tried that, but tried it again, and it didnt' help. I get the same Exception. **IOException: The process cannot access the file because it is being used by another process** – Lahib Sep 26 '12 at 07:08
  • I think you're not supposed to use FtpWebRequest.Abort, instead you should use Close, like here http://blog.logiclabz.com/c/ftp-file-upload-using-ftpwebrequest-in-net-c.aspx – Amr Sep 26 '12 at 07:19
  • In that example, he is not closing the **FtpWebRequest** bust the **FileStream** and the **Stream** – Lahib Sep 26 '12 at 07:36
  • you're right, sorry about that. My point is: the above mentioned sample should work. Try to modify the code to match it. – Amr Sep 26 '12 at 07:52
  • Ive tried modifying this code, many times. The Stream uploads the file, but as soon as i use the `Writer.Close()` or `using` the file is deleted from the FTP server. but cant i just use `Writer.Flush()` insted ?? – Lahib Sep 26 '12 at 09:02