Changing Remote File Permissions - Part 1
By: Rick Stevens
March 2nd 2005
Audience: Novice
Often times it is necessary to change permissions on one or more remote files. Secure FTP Factory for .NET easily allows you to issue commands directly to the FTP server.
Most FTP servers are configured to support additional commands, such as the SITE command. By using the Secure FTP Factory for .NET IssueCommand() method, you can send commands directly to your FTP server.
This series of articles will provide you with everything you need to:
- Issue the
chmod command for a set of image (.jpg) files, and
- Add a UI element to the example interface to modify remote file permissions.
Note
Some windows servers may translate the chmod command into a valid windows permissions command.
In this, part one of Changing Remote File Permissions, you'll learn how to use the IssueCommand() method to modify the permissions on a set of remote image files on a UNIX server. Take the case where more than one person (with unique privileges) is uploading images for your company web site and you want to modify the permissions.
You should have the appropriate login credentials for your FTP server in order to execute the chmod command.
You can download a free evaluation version of Secure FTP Factory for .NET. You can also download all the code you'll need for this article -- it will make things easier as we progress through the steps.
We'll begin by creating a new C# console application called FtpExample. In it we'll need to add the Jscape.Ftp.dll reference and include the following using statements. See the Secure FTP Factory for .NET User Guide for more information about adding a reference to your project.
using System;
using System.Collections;
using System.IO;
using System.Text;
using Jscape.Ftp;
Next, we'll create the basic structure for our console project.
using System;
using System.Collections;
using System.IO;
using System.Text;
using Jscape.Ftp;
namespace FtpExample {
public class FtpExample {
public FtpExample() {
}
[STAThread]
static void Main() {
}
}
}
Secure FTP Factory for .NET exposes many properties and methods that allow you to connect to an FTP server, trap various events, retrieve information from, and send commands to the server. Now that we have the basic structure in place, we can begin adding our code to accomplish these tasks.
The code necessary to create and ftp instance is straight-forward. The Ftp constructor provides many overloads to accommodate different needs. You can review the API documentation for more information. For us, we'll pass the hostname, username, and password as parameters to the constructor. You will need to pass in the appropriate parameters for your server.
using System;
using System.Collections;
using System.IO;
using System.Text;
using Jscape.Ftp;
namespace FtpExample {
public class FtpExample {
public FtpExample() {
Ftp myFtp = new Ftp("hostname","username","password");
// turn on debug mode
myFtp.Debug = true;
}
[STAThread]
static void Main() {
}
}
}
For this article, we only need to verify the connection to the server. The Jscape.Ftp component makes this easy by providing OnConnect and OnDisconnect delegates to which we can subscribe. You can find a listing of all delegates in the API reference.
using System;
using System.Collections;
using System.IO;
using System.Text;
using Jscape.Ftp;
namespace FtpExample {
public class FtpExample {
public FtpExample() {
Ftp myFtp = new Ftp("hostname","username","password");
// turn on debug mode
myFtp.Debug = true;
myFtp.FtpConnectedEvent += new FtpConnectEventHandler(OnConnected);
myFtp.FtpDisconnectedEvent += new FtpConnectEventHandler(OnDisconnected);
}
[STAThread]
static void Main() {
}
}
}
Now, we simply add the event handlers to our project to inform us of the current connection state.
using System;
using System.Collections;
using System.IO;
using System.Text;
using Jscape.Ftp;
namespace FtpExample {
public class FtpExample {
public FtpExample() {
Ftp myFtp = new Ftp("hostname","username","password");
// turn on debug mode
myFtp.Debug = true;
myFtp.FtpConnectedEvent += new FtpConnectEventHandler(OnConnected);
myFtp.FtpDisconnectedEvent += new FtpConnectEventHandler(OnDisconnected);
}
[STAThread]
static void Main() {
}
private void OnConnected(object sender, FtpConnectEventArgs e) {
Console.WriteLine("Connected to " + e.HostName + "\r\n");
}
private void OnDisconnected(object sender, FtpConnectEventArgs e) {
Console.WriteLine("Disconnected.");
}
}
}
We now have our completed structure and are ready to begin some real work.
Page 1 Page 2
|