Java and .NET components, FTP, TELNET, SMTP, POP3, IMAP, HTTP, SSH
Home Search Knowledge Base Support

Support

Click here for access to live sales support.

For technical support please submit a ticket to the Help Desk.

 

Java and .NET Help

iPod shuffle offer

For a limited time get the newly re-designed iPod shuffle free with qualifying purchase.

Click for details.


News

Secure iNet Factory 8.0 Released
04/04/2008 02:24 PM

Updates to Email Factory for .NET and Secure iNet Factory
03/29/2008 04:06 PM

JSCAPE Secure FTP Server 3.9 Preview
03/14/2008 12:19 PM

AnyClient Service and Application Launched
03/12/2008 03:41 PM

JSCAPE Secure FTP Server 3.8 Released
02/12/2008 10:50 AM


Tutorials

Email Validation with Java
04/15/2008 02:04 PM

Sending HTML Based Email Using Java
03/11/2008 02:47 PM

Secure FTP Using Java and FTPS (FTP over SSL)
03/10/2008 04:08 PM

FTP Directory Listing Using Java
03/10/2008 03:57 PM

Sending Email Using Java
03/09/2008 03:43 PM

SSH Using Java
03/09/2008 02:53 PM


Articles

DMZ File Transfer Streaming
03/28/2008 11:57 AM

Phishing looks to FTP to distribute malware
03/13/2008 05:14 PM

Ad Hoc File Transfer Explained
03/13/2008 09:16 AM

Password Policies Made Easy
03/12/2008 03:03 PM


Feedback

Request a feature or component

Request a Java or .NET component


 

Changing Remote File Permissions - Part 1

Page 1  Page 2

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

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."); } } }

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();

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.

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"));

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.

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"));

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."); } } }

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() {
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."); } } }

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.