Error in FTPing - reply 550 - The process cannot access the file because it is being used by another process.


  • Hi all,

    Here's the brief rundown:
    1. Business partners FTP encrypted text files to our site.
    2. We have a C# service use the FileSystemWatcher to parse the ftp folder (and subfolders) for these.
    3. Once we see that a new file has arrived, we open a connection to our internal FTP server and use the FTP.put method to move it over and then we delete the file.

    This exact code has been working fine on Windows 2000 Server.  Then the server blew up and we moved to Windows 2003 Server.  Now, during the FTP.Put we get the error "The process cannot access the file because it is being used by another process. "

    I used ProcessExplorer and saw that the C#service still had a grip on the file and attempting to manually delete the file while this service was on would result in a similar error.

    And we do have a WaitForExclusiveAccess method in place (i'll copy at the bottom)

    I'm pretty sure that when the partner puts the file up to our site that it's not locking the file because when I copy it from a local folder to the FTP folder, the same thing occurs.

    I'm thinking that it's some environmental issue between window 2000 and 2003 or maybe the Framework versions between the two machines, but unfortunately i cannot mirror the old server anymore as it is completely dead. 

    Anyone have any ideas?  I'm getting pretty desperate.

    Extra Info:

    Exact Error:

    Exception Type:
    Message: test4.txt.gpg: The process cannot access the file because it is being used by another process.
    Stack Trace: at reply, String[] expectedReplyCodes)
    at remoteFile, Boolean append)
    at srcStream, String remoteFile, Boolean append)
    at localPath, String remoteFile)
    at Company.Integration.Service.DMZ.DataArrivalsService.SendFile(String sourceFolderName, String sourceFileName, String destinationFolderName)
    at Company.Integration.Service.DMZ.DataArrivalsService.ProcessFile(String folderName, String fileName)
    at Company.Integration.Service.DMZ.DataArrivalsService.File_Created(Object sender, FileSystemEventArgs e)

    Code Snippet of where i think the error is occuring:


    // Wait for exclusive access

    WaitForExclusiveAccess(Path.Combine(folderName, fileName));

    // Transfer the file to the internal FTP server





    } //end method

    private void SendFile(string sourceFolderName, string sourceFileName, string destinationFolderName)


    // FTPClient ftp = new FTPClient(destinationFtpIPAddress);

    FTPClient ftp = new FTPClient(destinationFtpServerAddress, destinationFtpPort);

    string sourceFullPath = Path.Combine(sourceFolderName, sourceFileName);



    // Login and initialize the connection

    ftp.Login(userName, password);

    ftp.TransferType = FTPTransferType.BINARY;

    ftp.ConnectMode = FTPConnectMode.PASV;

    // Navigate to correct remote Warranty Integration root folder

    if (destinationFtpRelativePath != null && destinationFtpRelativePath.Length > 0)


    // Parse out subfolders

    string[] subFoldersNames = destinationFtpRelativePath.Split('/');

    foreach (string subFolderName in subFoldersNames)


    // Make sure folder name isn't empty (result of leading or trailing "/" in path)

    if (subFolderName.Length > 0)






    // Change to partner folder


    // Copy the file

    ftp.Put(sourceFullPath, sourceFileName);




    // Clean up



    // Give OS time to release locks on file


    // Remove the local source file after successfully copying the file to internal FTP server




    WaitForExclusiveAccess method:

    private void WaitForExclusiveAccess(string fileName, int waitSeconds, int retryPeriod)


    bool exclusiveAccessObtained = false;

    bool waitExpired = false;

    int retryCount = 0;

    DateTime startTime = DateTime.Now;





    // Open and close the file immediately

    FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);


    exclusiveAccessObtained = true;



    catch (IOException) { } // Ignore the IOException (it's likely to be a locking issue)

    TimeSpan elapsedTime = (DateTime.Now - startTime);

    waitExpired = (elapsedTime.TotalSeconds > waitSeconds);

    // Wait before trying again

    if (retryCount == 0)


    // First time through, try again in 1 second





    // After the first time, repeat every 'n' seconds

    System.Threading.Thread.Sleep(retryPeriod * 1000);



    } while (!exclusiveAccessObtained && !waitExpired);

    // If the wait expired, throw an exception

    if (waitExpired)


    throw new ApplicationException(string.Format("Total wait time of '{0}' seconds expired while attempting to gain exclusive access to file '{1}'.", waitSeconds, fileName));




    Wednesday, November 16, 2005 1:03 AM