Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Microsoft has created a new FTP service that has been completely rewritten for Windows Server® 2008. This new FTP service incorporates many new features that enable Web authors to publish content more easily than before, and offers Web administrators more security and deployment options.
The new FTP 7.5 service supports extensibility that lets you extend the built-in functionality that is included with the FTP service. More specifically, FTP 7.5 supports the creation of your own authentication and authorization providers. You can also create providers for custom FTP logging and for determining the home directory information for your FTP users.
This walkthrough will lead you through the steps to use managed code to create a simple FTP home directory provider.
The following items are required to complete the procedures in this article:
IIS 7 or above must be installed on your server, and the Internet Information Services (IIS) Manager must also be installed.
The new FTP 7.5 service must be installed.
You must create a root folder for FTP publishing.
You must use Visual Studio 2008.
Note
If you use an earlier version of Visual Studio, some of the steps in this walkthrough may not be correct.
A home directory for each user must be created; the code sample uses C:\Ftpusers\%*UserName*%
, but you could change that as necessary.
In this step, you will create a project in Visual Studio 2008 for the demo provider.
Open Microsoft Visual Studio 2008.
Click the File menu, then New, then Project.
In the New Project dialog box:
When the project opens, add the required references to the project:
Click Project, and then click FtpHomeDirectoryDemo Properties.
Click the References tab.
Add a reference path to the FTP extensibility library:
On the References tab, click the Reference Paths button.
Enter the path to the FTP extensibility assembly for your version of Windows, where C: is your operating system drive:
C:\Windows\assembly\GAC\_MSIL\Microsoft.Web.FtpServer\7.5.0.0\_\_31bf3856ad364e35
C:\Program Files\Reference Assemblies\Microsoft\IIS
Click Add Folder.
Click OK.
Add a reference to the FTP extensibility library for the project:
Add a reference to System.Web for the project:
Add a strong name key to the project:
Optional: You can add a custom build event to add the DLL automatically to the Global Assembly Cache (GAC) on your development computer:
Click Project, and then click FtpHomeDirectoryDemo Properties.
Click the Compile tab.
Click the Build Events button.
Enter the following in the Post-build event command line dialog box:
net stop ftpsvc
call "%VS90COMNTOOLS%\vsvars32.bat">null
gacutil.exe /if "$(TargetPath)"
net start ftpsvc
Click OK.
Save the project.
In this step, you will implement the extensibility interface for the demo provider.
Add the code for the authentication class:
In Solution Explorer, double-click the Class1.vb file.
Remove the existing code.
Paste the following code into the editor:
Imports System
Imports Microsoft.Web.FtpServer
Namespace FtpHomeDirectory
Public Class FtpHomeDirDemo
Inherits BaseProvider
Implements IFtpHomeDirectoryProvider
Function IFtpHomeDirectoryProvider_GetUserHomeDirectoryData( _
ByVal sessionId As String, _
ByVal siteName As String, _
ByVal userName As String) _
As String _
Implements IFtpHomeDirectoryProvider.GetUserHomeDirectoryData
' Note: You would add your own custom logic here.
' Return the user's home directory based on their user name.
Return ("C:\Ftpusers\" + userName)
End Function
End Class
End Namespace
Save and compile the project.
Note
If you did not use the optional steps to register the assemblies in the GAC, you will need to manually copy the assemblies to your IIS computer and add the assemblies to the GAC using the Gacutil.exe tool. For more information, see the Gacutil.exe (Global Assembly Cache Tool) article.
In this step, you will add the demo provider to your FTP service and the default Web site.
Determine the assembly information for the extensibility provider:
C:\Windows\assembly
path, where C: is your operating system drive.Add the extensibility provider to the global list of FTP providers:
Open the Internet Information Services (IIS) Manager.
Click your computer name in the Connections pane.
Double-click FTP Authentication in the main window.
Click Custom Providers in the Actions pane.
Click Register.
Enter FtpHomeDirectoryDemo for the provider Name.
Click Managed Provider (.NET).
Enter the assembly information for the extensibility provider using the information that you copied earlier. For example:
FtpHomeDirectoryDemo.FtpHomeDirectory.FtpHomeDirDemo,FtpHomeDirectoryDemo,version=1.0.0.0,Culture=neutral,PublicKeyToken=426f62526f636b73
Click OK.
Clear the FtpHomeDirectoryDemo check box in the providers list.
Click OK.
Add the custom provider to a site:
At the moment there is no UI that enables you to add custom features to a site, so you will have to use the following command line:
AppCmd set site "Default Web Site" /+ftpServer.customFeatures.providers.[name='FtpHomeDirectoryDemo',enabled='true']
Configure user isolation to use a custom provider:
At the moment there is no UI that enables you to specify custom features for user isolation, so you will have to use the following command line:
AppCmd set site "Default Web Site" /ftpServer.userIsolation.mode:Custom
In this walkthrough you learned how to:
When users connect to your FTP site, the FTP service will set each user's home directory to the path that you specified in the demo provider.
Training
Module
Work with files and directories in a .NET app - Training
Learn how to use .NET, C#, and System.IO to work with directories, paths, files, and the file system.