Dear Paul,
I tried to use u'r code but even though it is throwing "Not supported exception" .
This time I have tried the Different code in the vault it is successfull upto Connection but for uploading a file from the Device to pocket Pc it is Throwing Exception.
Particularly during the Binary Writer...in uploading
The Code IS as follows.
private void Connect()
{
FtpRequestCreator creator = new FtpRequestCreator();
WebRequest.RegisterPrefix( "ftp:", creator );
// Building our URI object
Uri testUri;
if( txtServer.Text.IndexOf( "ftp:" ) != 0 )
testUri = new Uri( "ftp://"+txtServer.Text );
else
testUri = new Uri( txtServer.Text );
// Creating a new FtbRequest object
request = (FtpWebRequest)WebRequest.Create( testUri );
request.Credentials = new NetworkCredential( txtUsername.Text, txtPassword.Text );
// Getting the Request stream
ftpRequestStream = request.GetRequestStream();
StreamReader reader = new StreamReader( ftpRequestStream );
txtServerCtrlResp.Text = reader.ReadToEnd();
}
private void btnConnect_Click(object sender, System.EventArgs e)
{
txtServerCtrlResp.Text = "";
txtServerDataResp.Text = "";
inputPanel1.Enabled = false;
Cursor.Current = Cursors.WaitCursor;
try
{
if( ftpRequestStream == null )
{
Connect();
btnConnect.Text = "Disconnect";
txtServer.Enabled = false;
txtUsername.Enabled = false;
txtPassword.Enabled = false;
btnSendCommand.Enabled = true;
txtCommand.Enabled = true;
// Setting default command to retrieval of remote files...
txtCommand.Text = "LIST";
}
else
{
CloseConnection();
btnConnect.Text = "Connect";
txtServer.Enabled = true;
txtUsername.Enabled = true;
txtPassword.Enabled = true;
btnSendCommand.Enabled = false;
txtCommand.Enabled = false;
}
}
catch( Exception err )
{
MessageBox.Show( err.Message );
}
finally
{
Cursor.Current = Cursors.Default;
}
}
private void BrowseButton_Click(object sender, System.EventArgs e)
{
// User wants to browse for the local file name. Let him, then
// enter the result into the LocalFilePathText.
// saveFileDialog1.Filter = "All files (*.*)|*.*";
// saveFileDialog1.FilterIndex = 1;
//if ( saveFileDialog1.ShowDialog() == DialogResult.OK )
if ( openFileDialog1.ShowDialog() == DialogResult.OK )
{
// Get the full file name and put it in the right field.
// LocalFilePathText.Text = saveFileDialog1.FileName;
LocalFilePathText.Text = openFileDialog1.FileName;
}
}
private void UploadButton_Click(object sender, System.EventArgs e)
{
// Upload the local file to the specified remote file.
try
{
StreamReader reader;
String commandReply;
// Open the input file. If the file does not exist, it's an
// error.
FileStream fs = new FileStream( LocalFilePathText.Text, FileMode.Open );
// Create the reader for the local file data.
BinaryReader r = new BinaryReader( fs );
// Opening the data connection, this must be done before
// we issue the command.
Stream ftpResponseStream = request.GetResponse().GetResponseStream();
BinaryWriter response = new BinaryWriter( ftpResponseStream );
// Prepare to send commands to the server.
StreamWriter writer = new StreamWriter( ftpRequestStream );
// Set transfer type to IMAGE (binary).
writer.Write( "TYPE I\r\n" );
writer.Flush();
// Reading the request output
reader = new StreamReader( ftpRequestStream );
commandReply = reader.ReadToEnd();
// Write the command to the request stream.
String cmd = "stor " + RemoteFilePathText.Text + "\r\n";
writer.Write( cmd );
writer.Flush(); // We MUST flush before we start reading from
// both response and request
// Reading the request output
commandReply = reader.ReadToEnd();
// Allocate buffer for the data, which will be written in blocks.
int bufsize = 1024;
byte[] buf = new byte[ bufsize ];
int xcount;
while ( ( xcount = r.Read( buf, 0, bufsize ) ) > 0 )
{
// Send next buffer over the data connection.
response.Write( buf, 0, xcount );
}
r.Close();
fs.Close();
response.Close();
reader.Close();
}
catch( Exception err )
{
MessageBox.Show( err.Message );
}
}
private void DownloadButton_Click(object sender, System.EventArgs e)
{
// Download the remote file to the specified local file.
try
{
StreamReader reader;
String commandReply;
// Create the output file. If the file already exists,
// truncate it.
FileStream fs = new FileStream( LocalFilePathText.Text, FileMode.Create );
// Create the writer for data.
BinaryWriter w = new BinaryWriter( fs );
// Opening the data connection, this must be done before
// we issue the command.
Stream ftpResponseStream = request.GetResponse().GetResponseStream();
BinaryReader response = new BinaryReader( ftpResponseStream );
StreamWriter writer = new StreamWriter( ftpRequestStream );
// Set transfer type to IMAGE (binary).
writer.Write( "TYPE I\r\n" );
writer.Flush();
// Reading the request output
reader = new StreamReader( ftpRequestStream );
commandReply = reader.ReadToEnd();
// Write the command to the request stream.
String cmd = "retr " + RemoteFilePathText.Text + "\r\n";
writer.Write( cmd );
writer.Flush(); // We MUST flush before we start reading from
// both response and request
// Reading the request output
commandReply = reader.ReadToEnd();
// Allocate buffer for the data, which will be read in blocks.
int bufsize = 1024;
byte[] buf = new byte[ bufsize ];
int xcount;
while ( ( xcount = response.Read( buf, 0, bufsize ) ) > 0 )
{
// Write the next buffer to the file.
w.Write( buf, 0, xcount );
}
w.Close();
fs.Close();
response.Close();
reader.Close();
}
catch( Exception err )
{
MessageBox.Show( err.Message );
}
}
Pls find how this can be sorted.....
Thanks
sundar