Changing Remote File Permissions - Part 1
Page 1 Page 2
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);
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.");
}
}
}
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();
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.");
}
}
}
Figure 1 shows an example result of the GetDirListingAsString("*.jpg") method.
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"));
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.");
}
}
}
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.
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"));
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.");
}
}
}
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() {
FtpExample ftpExample = new FtpExample();
}
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.");
}
}
}
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.
|