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
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