Originally written by Rick Stevens on 2nd March 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
chmodcommand 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.
Setup
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;
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.
Instantiating the Ftp class
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() {
}
[STAThread]
static void Main() {
}
}
}
Subscribing to and Trapping events
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;
}
[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() {
}
Connecting to the server
We only need to invoke the Connect() method to connect to the FTP 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;
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.");
}
}
}
Retrieving remote directory contents
The information we are looking for in this article relates to the contents of a remote directory containing image files. There are two methods we can use to retrieve this information, GetDirListingAsString() and GetDirListing(). Both methods may pass a standard file filter as an optional parameter to limit the returned value. You can review the API documentation for more information about these to methods.
Lets display the current listing of the .jpg image files to the console. To do so, we use the GetDirListingAsString() method passing in the file filter "*.jpg".
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);
myFtp.Connect();
}
[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.");
}
}
}
Figure 1 shows an example result of the GetDirListingAsString("*.jpg") method.
Changing the file permissions
We'll modify permission for these image files to allow only Owner and Group access. More specifically, we want the owner and group to have full permissions and other users to be denied access. On the UNIX server, these permissions are designated as 770 and are displayed as -rwxrwx--- in the console.
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);
myFtp.Connect();
Console.WriteLine(myFtp.GetDirListingAsString("*.jpg"));
}
[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.");
}
}
}
Notice that we used the GetDirListing() method to return a collection of FtpFile objects that match our filter criteria. We then issue the chmod command to the server for each iteration through the collection.
Figure 2 shows an example result after issuing the chmod command.
Disconnecting from the server
Now we should disconnect from the server. It is always good practice to specifically disconnect from a server rather than relying on an automated process to dispose of the connection for you.
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);
myFtp.Connect();
Console.WriteLine(myFtp.GetDirListingAsString("*.jpg"));
IEnumerator e = myFtp.GetDirListing();
while (e.MoveNext()) {
FtpFile file = (FtpFile)e.Current;
myFtp.IssueCommand("SITE chmod 770 " + file.Filename);
}
Console.WriteLine("New permissions:\r\n"+myFtp.GetDirListingAsString("*.jpg"));
}
[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.");
}
}
}
Testing our example
Add the following highlighted code to your example project. Make sure you have the startup project property set, and Press F5. You may want to add a breakpoint to the Disconnect() method in order to see the results in the console before the application terminates.
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);
myFtp.Connect();
Console.WriteLine(myFtp.GetDirListingAsString("*.jpg"));
IEnumerator e = myFtp.GetDirListing();
while (e.MoveNext()) {
FtpFile file = (FtpFile)e.Current;
myFtp.IssueCommand("SITE chmod 770 " + file.Filename);
}
Console.WriteLine("New permissions:\r\n"+myFtp.GetDirListingAsString("*.jpg"));
myFtp.Disconnect();
}
[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.");
}
}
}
Conclusion
As you can see from the console display, we changed the permissions for the image files.
In the next article we'll add a user interface to the example application to modify the permissions of remote directories and files. The example application we will be using is provided in the free evaluation version of Secure FTP Factory for .NET
Comments