Permissions of remote FTP files and directories

Introduction

Using the Ultimate .NET FTP Library, you can get and set permissions of a single file or directory or multiple items at once. Today we will go through all the steps to guide you on how to achieve that.

FTP Permissions

FTP protocol initially did not have the concept of file/folder permissions build-in directly. To list all files and directories, you would need to execute the appropriate command. LIST command lists the content of the remote server. The commands on well-known operating systems are as follows:

  • UNIX/LINUX: ls (returns three permissions, user permissions, group permissions, and other permissions)
  • WINDOWS: dir

The Ftp class exposes flexible event-driven methods to process remote files and directories like uploading, downloading, setting file attributes, etc. Ultimate FTP provides both methods for setting permissions of a single file or multiple files. Before setting permissions of remote files, you may need to know how to upload files to an FTP server.

Note:

  • Not all FTP servers support this operation.
  • Our component does not support setting local files permissions

Get Permissions of Remote Files and Directories

To read directory content using UltimateFtp, [Ftp.ListDirectory][2] method can be used. UltimateFtp implements various Internet RFC specifications including RFC3659. MLSD command was introduced with RFC 3659. MLSD standardized the way listings are returned from the server. There are various overloaded methods, to retrieve files and directories in the current folder, bypassing argument for MLSD/LIST command, passing search condition, etc. This method helps to retrieve permissions information. This method uses MLSD commands if the FTP server supports this feature or else otherwise uses standard LIST command.

Set Permission of a single Remote File

By using the SetFilePermissions method, you can set permissions of a single file. The following code snippet shows you how to set permissions of a single file.

Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:

C#:

using System;
using ComponentPro.Net;

VB.NET:

Imports ComponentPro.Net

Create a new instance of the Ftp class. Now you can connect to the FTP server with Connect methods. The code looks similar to the following:

C#:

// Create a new class instance.
Ftp client = new Ftp();
// Connect to the FTP server.
client.Connect("myserver");

VB.NET:

' Create a new class instance. 
Dim client As New Ftp()
' Connect to the FTP server.
client.Connect("myserver")

Use your user name and password to login with one of Authenticate methods.  The code looks similar to the following:

C#:

// Authenticate.
client.Authenticate("userName", "password");

VB.NET:

' Authenticate.
client.Authenticate("userName", "password")

Do your work like setting required permission.  The code looks similar to the following:

C#:

// Set file permission to OwnerRead + OwnerWrite.
client.SetFilePermissions("/file.dat", FtpFilePermissions.OwnerRead | FtpFilePermissions.OwnerWrite);

VB.NET:

' Set file permission to OwnerRead + OwnerWrite.
client.SetFilePermissions("/file.dat", FtpFilePermissions.OwnerRead Or FtpFilePermissions.OwnerWrite)

After completing your work, Disconnects the FTP client from the FTP server to which it is connected.

C#:

// Disconnect.
client.Disconnect();

VB.NET:

' Disconnect.
client.Disconnect()

Set permissions of multiple files and directories

To set permissions of multiple files, use the SetMultipleFilesPermissions method of the Ftp class. The following example shows you how easy it is to set permissions of multiple files:

C#:

// Set files' permissions to OwnerRead + OwnerWrite recursively.
client.SetMultipleFilesPermissions("/", FtpFilePermissions.OwnerRead | FtpFilePermissions.OwnerWrite, true, new NameSearchCondition("*.dat"));

VB.NET:

' Set files' permissions to OwnerRead + OwnerWrite recursively.
client.SetMultipleFilesPermissions("/", FtpFilePermissions.OwnerRead Or FtpFilePermissions.OwnerWrite, True, New NameSearchCondition("*.dat"))

FtpFilePermissions is an enumeration containing Unix file permissions with value described below:

Member Description
None No permissions.
PublicExecute Others execute.
PublicWrite Others write.
PublicRead Others read.
GroupExecute Group execute.
GroupWrite Group write.
GroupRead Group read.
OwnerExecute Owner execute.
OwnerWrite Owner write.
OwnerRead Owner read.
Sticky Sticky.
SetGid Set GID bit.
SetUid Set UID bit.

45-Day Money Back Guarantee

We will refund your full money in 45 days
if you are not satisfied with our products

Buy Now

Dont miss out Get update on new articles and other opportunities