Downloading specific files type from FTP in a folder using vb.net Answered RSS 2 replies Last post Feb 06, 2013 04:28 AM by geniusvishal.
-->Definition
Gets or sets the filter string that determines what types of files are displayed from either the OpenFileDialog or SaveFileDialog.
Property Value
A String that contains the filter. The default is Empty, which means that no filter is applied and all file types are displayed.
Exceptions
The filter string is invalid.
Examples
The following examples demonstrate several types of filter strings that can be set by using the Filter property.
Remarks
If Filter is either
null
or Empty, all files are displayed, and folders are always displayed.You can specify a subset of file types to be displayed by setting the Filter property. Each file type can represent a specific type of file, such as the following:
- Word Documents (*.doc)
- Excel Worksheets (*.xls)
- PowerPoint Presentations (*.ppt)
Alternatively, a file type can represent a group of related file types, such as the following:
- Office Files (*.doc, *.xls, *.ppt)
- All Files (*.*)
To specify a subset of the types of files that are displayed, you set the Filter property with a string value (the filter string) that specifies one or more types of files to filter by. The following shows the expected format of the filter string:
FileType1[[|FileType2]...[|FileTypeN]]
You use the following format to describe each file type:
Label|Extension1[[;Extension2]...[;ExtensionN]]
The Label part is a human-readable string value that describes the file type, such as the following:
- 'Word Documents'
- 'Excel Worksheets'
- 'PowerPoint Presentations'
- 'Office Files'
- 'All Files'
Each file type must be described by at least one Extension. If more than one Extension is used, each Extension must be separated by a semicolon (';'). For example:
- '*.doc'
- '*.xls;'
- '*.ppt'
- '*.doc;*.xls;*.ppt'
- '*.*'
The following are complete examples of valid Filter string values:
Word Documents|*.doc
Excel Worksheets|*.xls
PowerPoint Presentations|*.ppt
Office Files|*.doc;*.xls;*.ppt
All Files|*.*
Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt|Office Files|*.doc;*.xls;*.ppt|All Files|*.*
Each file type that is included in the filter is added as a separate item to the Files of type: drop-down list in the OpenFileDialog or SaveFileDialog, as shown in the following figure.
The user can choose a file type from this list to filter by. By default, the first item in the list (for example, the first file type) is selected when the OpenFileDialog or SaveFileDialog is displayed. To specify that another file type to be selected, you set the FilterIndex property before showing the OpenFileDialog or SaveFileDialog (by calling ShowDialog).
Applies to
See also
2 Oct 2008CPOL
Dialog for selecting a file found on an FTP server.
Introduction
I don't know if this dialog will be helpful to anyone else, but I am writing an application for interfacing with websites, editing them, and so forth, and I need a text box in the application populated with a link to a resource. At first, I just left it to the user to properly type in the resource URL, but then I got to thinking that it would be nice to be able to browse to the resource, choose it, then have the text box populated with the link based on the chosen file. This dialog achieves this.
Using the code
There are two constructors available for the dialog:
FtpBrowseDialog();
FtpBrowseDialog(String host, String startpath, int port, String username, String password, bool passivemode);
The base constructor will open the dialog, with a nested dialog prompting for connection variables:
- Server (host) - enter the domain without 'ftp://', the dialog will add this for you.
- Start path (optional) - controls the subdirectory that the dialog starts in.
- Port - TCP port to be used, default FTP port is 21.
- Username - username for connection.
- Password - password for connection.
- Use Passive checkbox - controls whether connecting to a passive FTP or not, default is checked for yes.
![Dialog Dialog](/uploads/1/2/6/4/126493943/779457459.jpg)
The other constructor allows you to pass all these variables, suppressing the connection prompt.
Once connected, another subdialog will show, with a marquee progress bar while the FTP file structure is loaded. Once loaded, the progress dialog will close, and access to the file structure will be available.
If you would like to see how the UI works without loading a server, you can use the
TestMode
property. Set this to true
before calling ShowDialog()
, and a mock file structure will be loaded that you can use for testing purposes.Here is the basic working of the UI. I tried to mimic the
OpenFileDialog
as much as I could, but there are some features still lacking I'm sure; any suggestions are welcome for future revisions:- Selecting a folder on the left will populate that folder's subfolders and files on the right.
- The OK button remains disabled until an actual file is selected in the right panel.
- Two toolbar buttons are provided:
- The button to the left moves up a directory, and disables once the root folder is reached.
- The button to the right allows you to open a new server structure, the connection details subdialog will show, defaulting to the current connection information, but will allow you to enter new information and click Connect to load a new server.
- Double clicking on a file in the right panel will automatically select the file and close the dialog.
- Double clicking a subfolder in the right panel will move to that folder.
- When the right panel is focused, pressing backspace will mimic the up directory toolbar button.
Once either a file is double clicked, or the OK button is pressed, then
DialogResult.OK
is returned and the file info can be collected from the calling form. There are three options for acquiring the selected file information:FtpBrowseDialog.SelectedFile
- returns file in FQN form with FTP references.FtpBrowseDialog.SelectedFileName
- returns the file name only.FtpBrowseDialog.SelectedFileUrl
- returns the file in a web friendly fashion for link purposes (note: this also replaces all spaces in the file name with %20).
Points of interest (My approach)
If you are new to programming, you may be asking 'How did he do this?!?' And if you are a pro, you will probably be wondering 'Why did he do this?!?'
Here are the basics of the project. First, I created a new
TreeNode
class called FtpTreeNode
, and gave it a few extra properties and methods:FtpTreeNode.Files
- AStringCollection
holding all the file names for the folder this node represents.FtpTreeNode.Directories
- AStringCollection
holding all the subdirectory names for the folder this node represents.
FtpTreeNode.AddFile(String file)
- adds a file to the collection.FtpTreeNode.AddDirectory(String dir)
- adds a directory to the collection.FtpTreeNode.Select()
- handles minute details involved with what needs to happen when a node is selected.
FTP information is collected by juggling between an
FtpWebRequest
and an FtpWebResponse
. You may have stumbled on this looking for an answer to how to control the port variable of a FtpWebRequest
, without having to hard code it into the connection string using the ':' character. Well, the answer is simple, but took me a bit to find. Using an instance of UriBuilder
will accomplish this. With a UriBuilder
, you supply the protocol type 'ftp' and the host domain, along with the port number and a starting path string. There are a few different constructors which will allow some of these specifics to be optional. Here is an example of the URI that I built for populating the dialog.I had to use
startpath
because I wanted only the root node to have the FQN, and each subnode to only show the directory name.Something else I had to figure out was that
FtpWebRequest
has a UsePassive
property that controls whether to connect using PASV or not. Older servers do not use passive mode, most new ones do. Also, you can control the username and password by setting the Credential
property of the FtpWebRequest
.Now for the fun part. I had to use method recursion to accomplish the file structure load. There is a method in
FtpBrowseDialog
called LoadSubNodes
.This method sets
request.Method
to WebRequestMethods.Ftp.ListDirectroyDetails
, gets a response based on the supplied node's FTP URL, and then parses the response to get all the directories and files added to the node, and then creates a subnode for each directory and passes it back into LoadSubNodes
, that is where the recursion comes in. Once all sub directory nodes are loaded and added into this node's subnodes, then the method returns. All that needs to be done to kick this party off is to create a root node and pass it into LoadSubNodes
; once that returns, add the root node to the TreeView
.History
- October 2, 2008 - v1.0 - Initial post.
Next version plans:
- Allow disabling of the Open New Server button, allowing you to lock a user to a specified server.
- Make it easier to add custom images to the file list panel for different file types.
- Provide a file filter property, allowing you to control which file types are supported by the dialog.