Changing Remote File Permissions - Part 2
By: Rick Stevens
April 3rd 2005
Audience: Novice
In Changing Remote File Permissions Part 1 we used the Secure FTP Factory for .NET IssueCommand() method to modify the permission settings of files on the remote Unix server.
This article expands on that concept by adding a user interface to the example code installed with Secure FTP Factory for .NET. 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.
Important The information in this article pertains to a Unix server only. In addition, specific permission types, such as Set group ID on execution (SGID) and "Sticky-bit" are not addressed.
In order to add a graphical permission interface we need to do two things,
- Modify the original example interface to display the existing permissions, and
- Add a UI form to modify the permissions of the selected files.
Begin by opening the C# example project. By default, this project is located in the C:\Program Files\JSCAPE\Secure FTP Factory for .NET\Examples\C# directory. You can also use the Program Menu shortcut created when you installed Secure FTP Factory for .NET to open this project.
You'll notice that the example interface (FormMain.cs) contains two file list views, one for the local directory contents, and the other for the remote directory contents. We need to update the remote directory listing to display the existing file permissions. To do so, we need to modify the InitListView() method, and the references to it, and the GetRemoteListing() method to accomodate the file attributes.
The InitListView() method is responsible for initializing both the local and remote file listings. We'll need to add a flag to determine whether the file attributes column should be displayed. It is a simple matter of adding a boolean parameter and testing the condition of that paramter. If the flag is true, then we should add the column to contain the file permissions. If it is false, then the column is not added to the listing.
Add the highlighted code to the InitListView() method as shown.
protected void InitListView(ListView lvFiles, bool showPerms) {
lvFiles.Clear();
lvFiles.Columns.Add("Name",-2,System.Windows.Forms.HorizontalAlignment.Left);
if (showPerms) {
lvFiles.Columns.Add("Attributes",-2,System.Windows.Forms.HorizontalAlignment.Left);
}
}
Now we need to modify the references to the InitListView() method to pass the showPerms parameter. The example code has two references, one each in the GetLocalListing() and GetRemoteListing() methods.
Add the showPerms flag to the local listing method as shown in the following code segment.
private void GetLocalListing(string localPath) {
// add listview headers
InitListView(listViewLocal, false);
Add the showPerms flag to the remote listing method as shown in the following code segment.
private void GetRemoteListing(string remotePath) {
// add listview headers
InitListView(listViewRemote, true);
Now that we have modified the InitListView() method (and its references) we can add the code to display the permissions in the GetRemoteListing() method. Add the highlighted code to the GetRemoteListing() method as shown in the following code segment.
int ico = 0;
string remoteFilePermissions = "";
try {
if (myFtp.RemoteDir != "/") {
if (textRemoteDir.Text.Length > 1) {
ListViewItem lvItem = new ListViewItem("..", 0);
listViewRemote.Items.Add(lvItem);
}
}
IEnumerator remoteList = myFtp.GetDirListing();
while(remoteList.MoveNext()) {
FtpFile remoteFile = (FtpFile)remoteList.Current;
remoteFilePermissions = remoteFile.Permission;
if (remoteFile.IsDirectory) {
ico = 0;
}else if (remoteFile.Link) {
ico = 2;
}else{
ico = 1;
}
ListViewItem lvItem = new ListViewItem(remoteFile.Filename, ico);
lvItem.SubItems.Add(remoteFilePermissions);
listViewRemote.Items.Add(lvItem);
}
Compile the example project and press F5. You will need to specify the connection criteria specific to your Ftp server, or you can use the anonymous Ftp server provided by Netscape. The connection information is ftp.netscape.com, user = anonymous. Once the connection is established, you should see the attributes column header added to the remote file listing view.
Figure 1 shows an example result of the attributes column added to the remote listing.
Note
You will not be able to alter the contents of the Netscape server. You will need have the appropriate login credentials for your Ftp server in order to complete the next section.
Next, we'll add the code to manipulate the permission settings we want to apply to the remote directory files.
Page 1 Page 2 Page 3
|