CSharp/C# >> FtpWebRequest DeleteFile Problem

by Waldy » Fri, 29 Aug 2008 17:38:42 GMT

Hi there,
I have written a service in VS2005 that downloads files from
an FTP site processes them and then deletes the files if successfully
processed. It all works fine when run in our test lab with an FTP site that
I setup on IIS. However, I have an issue at the customer site. Their FTP
server is on Linux, but I presume that is not an issue. What happened the
first time I ran it was that it deleted every other file. It just so
happened that there were files of about 60k and files of about 600k. The
small files were deleted, but the large ones were not. A WebException error
occured for the second, fourth e.t.c. files. When I ran it again, only the
larger files were left in the FTP folder, and only the first one was
deleted. I guess it may be sone sort of timing issue..



CSharp/C# >> FtpWebRequest DeleteFile Problem

by Ignacio Machin ( .NET/ C# MVP ) » Fri, 29 Aug 2008 21:24:19 GMT



what says the exception?
Can you delete those files if you connect from the system ftp? maybe
you do not have permission to delete them

CSharp/C# >> FtpWebRequest DeleteFile Problem

by Goran Sliskovic » Mon, 01 Sep 2008 20:46:31 GMT


that
error
the

Can you please check exact exception? Is it maybe "bad sequence of
commands"?

Regards,
Goran

CSharp/C# >> FtpWebRequest DeleteFile Problem

by Waldy » Tue, 02 Sep 2008 16:28:45 GMT


Hi Goran,
here is the error:

Exception: System.Net.WebException
Message: The remote server returned an error: (500) Syntax error, command
unrecognized.
Source: System
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.Net.FtpWebRequest.RequestCallback(Object obj)
at System.Net.CommandStream.Abort(Exception e)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetResponse()

CSharp/C# >> FtpWebRequest DeleteFile Problem

by Pavel Minaev » Tue, 02 Sep 2008 17:00:45 GMT


It might help if you use a packet sniffer to get the actual log of the FTP
session - what commands go to the server, and what the responses to each
are.

CSharp/C# >> FtpWebRequest DeleteFile Problem

by Goran Sliskovic » Tue, 02 Sep 2008 20:00:39 GMT


command

Yes, sniffer should reveal the problem. I had similar problem with UNIX
based system and FTPWebRequest. FTPWebRequest is cacheing connections to FTP
server and reuses the connection between commands. However, it resend
USER/PASS commands even on already connected and authorized connection (and
that is allowed according to standard) and UNIX server refuses it (it's an
embedded system). I end up with "bad sequence of commands", which is bit
different than what original poster gets.

Wireshark is free and powerfull sniffer, if we see the trace I guess will
find the problem.

Regards,
Goran

CSharp/C# >> FtpWebRequest DeleteFile Problem

by Waldy » Wed, 03 Sep 2008 23:04:03 GMT


Hi Goran,
according to the trace, the response is returning 500
Unknown Command from a User request. So it appears to be the problem that
you talked about. How did you get round it.

CSharp/C# >> FtpWebRequest DeleteFile Problem

by Goran Sliskovic » Thu, 04 Sep 2008 05:06:56 GMT


Hi,
I gave up FTPWebRequest... Found some ugly FTP client code on the internet,
fixed few bugs and it's working now. You may try to set keepalive to false,
however that has some side effects (reconnect on every command).

Regards,
Goran

CSharp/C# >> FtpWebRequest DeleteFile Problem

by Waldy » Thu, 04 Sep 2008 22:12:08 GMT


Good man! That's fixed it for me.

CSharp/C# >> FtpWebRequest DeleteFile Problem

by Goran Sliskovic » Fri, 05 Sep 2008 00:17:05 GMT


Keep in mind that this will (probably) open/close TCP connection for every
command. If you have lot of commands (eg. files to delete) this is not free.
It will depend on the network infrastructure/project you are working on
whether this turns to real problem. You could possibly be cut by routers in
between as a result of false attack detection in some cases. It sucked for
me (connecting to embedded system over slow/high latency links). It is
relativly cheap to create interface and allow implementation to change. But
that depends on your project requirements.

Regards,
Goran

Similar Threads

1. FTPWebRequest Problem

If I create an MFC application in Visual Studio 6 using the following code:

	CInternetSession inet_session;
	CFtpConnection *ftp_connection;

	ftp_connection = inet_session.GetFtpConnection( "Integration300", "guest", 
"ihannah", 21,TRUE);

	CString CurrentDirectory;
	ftp_connection->GetCurrentDirectory(CurrentDirectory);

then the FTP connection is made and works as expected.

If I then try and use the following code (C#, Visual Studio 2005) I always 
get a timeout:

      // Get the object used to communicate with the server.
      FtpWebRequest request = 
(FtpWebRequest)WebRequest.Create(@"ftp://integration300");
      request.Credentials   = new NetworkCredential("guest","ihannah");
      request.Proxy = null;
      request.EnableSsl     = false;
      request.UseBinary     = true;
      request.KeepAlive     = false;
      request.Method        = WebRequestMethods.Ftp.ListDirectoryDetails;
   
      FtpWebResponse response = (FtpWebResponse)request.GetResponse();
      response.Close();

Does anyone have any idea why this is the case?

Regards

-- 
Ian H

2. FTP Upload using FtpWebRequest problem PLEASE HELP - CSharp/C#

3. Asynchronous uploading problem with FtpWebRequest

dear friends
   I have to write a program of multiple upload of images.

I have used the concept of Asynchronous through net. But It is giving
error. Here first I am using single image file upload.

when it reaches to this line
  response = (FtpWebResponse)state.Request.EndGetResponse(ar);
It is written in function EndGetResponseCallback

then it is giving error
The remote server returned an error: (550) File unavailable (e.g.,
file not found, no access).

please help me it is very urgent.

I am showing my whole code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;


namespace AsynchronousFtpUpLoader
{
    public class FtpState
    {
        private ManualResetEvent wait;
        private FtpWebRequest request;
        private string fileName;
        private Exception operationException = null;
        string status;

        public FtpState()
        {
            wait = new ManualResetEvent(false);
        }

        public ManualResetEvent OperationComplete
        {
            get { return wait; }
        }

        public FtpWebRequest Request
        {
            get { return request; }
            set { request = value; }
        }

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }
        public Exception OperationException
        {
            get { return operationException; }
            set { operationException = value; }
        }
        public string StatusDescription
        {
            get { return status; }
            set { status = value; }
        }
    }

    public class AsynchronousFtpUpLoader
    {
        static void Main(string[] args)
        {
            // Create a Uri instance with the specified URI string.
            // If the URI is not correctly formed, the Uri constructor
            // will throw an exception.
            ManualResetEvent waitObject;

            ////Uri target = new Uri(args[0]);
            string fileName = @"C:\data\1.jpg";
            FtpState state = new FtpState();
            FtpWebRequest request =
(FtpWebRequest)WebRequest.Create("ftp://192.168.12.19/vivekNew/
vivek");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example uses anonymous logon.
            // The request is anonymous by default; the credential
does not have to be specified.
            // The example specifies the credential only to
            // control how actions are logged on the server.

            request.Credentials = new NetworkCredential("uninumber",
"chetu123");

            // Store the request in the object that we pass into the
            // asynchronous operations.
            state.Request = request;
            state.FileName = fileName;

            // Get the event to wait on.
            waitObject = state.OperationComplete;

            // Asynchronously get the stream for the file contents.
            request.BeginGetRequestStream(
                new AsyncCallback(EndGetStreamCallback),
                state
            );

            // Block the current thread until all operations are
complete.
            waitObject.WaitOne();

            // The operations either completed or threw an exception.
            if (state.OperationException != null)
            {
                throw state.OperationException;
            }
            else
            {
                Console.WriteLine("The operation completed - {0}",
state.StatusDescription);
            }

        }
        private static void EndGetStreamCallback(IAsyncResult ar)
        {
            FtpState state = (FtpState)ar.AsyncState;

            Stream requestStream = null;
            // End the asynchronous call to get the request stream.
            try
            {
                requestStream = state.Request.EndGetRequestStream(ar);
                // Copy the file contents to the request stream.
                const int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];
                int count = 0;
                int readBytes = 0;
                FileStream stream = File.OpenRead(state.FileName);
                do
                {
                    readBytes = stream.Read(buffer, 0, bufferLength);
                    requestStream.Write(buffer, 0, readBytes);
                    count += readBytes;
                }
                while (readBytes != 0);
                Console.WriteLine("Writing {0} bytes to the stream.",
count);
                // IMPORTANT: Close the request stream before sending
the request.
                requestStream.Close();
                // Asynchronously get the response to the upload
request.
                state.Request.BeginGetResponse(
                    new AsyncCallback(EndGetResponseCallback),
                    state
                );
            }
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine("Could not get the request
stream.");
                state.OperationException = e;
                state.OperationComplete.Set();
                return;
            }

        }

        // The EndGetResponseCallback method
        // completes a call to BeginGetResponse.
        private static void EndGetResponseCallback(IAsyncResult ar)
        {
            FtpState state = (FtpState)ar.AsyncState;
            FtpWebResponse response = null;
            try
            {
                response =
(FtpWebResponse)state.Request.EndGetResponse(ar);
                response.Close();
                state.StatusDescription = response.StatusDescription;
                // Signal the main application thread that
                // the operation is complete.
                state.OperationComplete.Set();
            }
            // Return exceptions to the main application thread.
            catch (Exception e)
            {
                Console.WriteLine("Error getting response.");
                state.OperationException = e;
                state.OperationComplete.Set();
            }
        }

    }
}


Thanks in advance

4. Problem Uploading File Using FtpWebRequest - Asp.Net

5. Computer.FileSystem.DeleteFile not working over network

Hi all

I've got an ASP.Net 2.0 web application, where users are uploading files to 
the web server from a C drive on a networked pc.

The upload and save work fine, but once done, I want the original file on 
the pc D drive to be deleted.

The code I'm using is:

My.Computer.FileSystem.DeleteFile(Me.newCalibrationRecord.RecordLocation)

Me.newCalibrationRecord.RecordLocation is "C:\DirectoryName\Filename".

I get an error message saying the file cannot be found despite the fact that 
I know it's there. I've also checked permissions and the ASP.NET account has 
the correct permissions to the pc's C drive.

Is it something to do with the fact that the application is looking on the 
webserver's C drive not the user's local machine?

Thanks in advance
Julia


6. Rename File, DeleteFile, Create Directory in CF2.0

7. FTPWebRequest Problem

If I create an MFC application in Visual Studio 6 using the following
code:

	CInternetSession inet_session;
	CFtpConnection *ftp_connection;

	ftp_connection = inet_session.GetFtpConnection( "Integration300",
"guest", "ihannah", 21,TRUE);

	CString CurrentDirectory;
	ftp_connection->GetCurrentDirectory(CurrentDirectory);

then the FTP connection is made and works as expected.

If I then try and use the following code (C#, Visual Studio 2005) I
always get a timeout:

      // Get the object used to communicate with the server.
      FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(@"ftp://integration300");
      request.Credentials   = new NetworkCredential("guest","ihannah");
      request.Proxy = null;
      request.EnableSsl     = false;
      request.UseBinary     = true;
      request.KeepAlive     = false;
      request.Method        =
WebRequestMethods.Ftp.ListDirectoryDetails;

      FtpWebResponse response = (FtpWebResponse)request.GetResponse();
      response.Close();

Does anyone have any idea why this is the case?

Regards

Ian Hannah

8. FTPWebRequest problem - .Net Framework