65.9K
CodeProject 正在变化。 阅读更多。
Home

以编程方式创建 FTP 和 Web IIS 虚拟目录

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.13/5 (10投票s)

2006年2月5日

2分钟阅读

viewsIcon

113112

downloadIcon

2061

使用 C# 以编程方式创建 FTP 和 Web IIS 虚拟目录。

Sample Image - VirtualDirectoryCreation.gif

引言

本文将为您提供一个开始,以在 Internet 信息服务器中创建 FTP 或 Web IIS 虚拟目录。当我在 .NET 平台上部署 Web 解决方案时,我惊讶地发现我无法自定义安装程序以提供与 IIS 根目录不同的物理路径,出于安全原因。所以我编写了一个不同的安装程序类,并通过提示用户输入虚拟目录的名称以及安装源文件的路径或要与虚拟目录映射的目录来自定义安装。这个类提供了创建和删除虚拟目录的两种方法。最初我是为 IIS Web 目录编写的,后来我将其扩展到 FTP 虚拟目录。

使用代码

为了使用该类,请添加对 DLL System.DirectoryServices 的引用,并在您的类中导入命名空间 VirtualDirectoryCreation

然后创建 VdirLib 类的对象。该类包含四个属性和一个 enum 类型。

enum VDirType 保存要创建的虚拟目录的类型。 它可以是 FTP 虚拟目录或 Web IIS 虚拟目录。

public enum VDirType
{
    FTP_DIR, WEB_IIS_DIR
};

类型为 enum VDirType 的属性 strDirectoryType 返回或设置要创建的虚拟目录。 它可以是 FTP 或 Web IIS 虚拟目录。

private VDirType _strDirectoryType;

public VDirType strDirectoryType
{
    get
    {
        return _strDirectoryType;
    }
    set
    {
        _strDirectoryType = value;
    }
}

属性 strPhysicalPath 获取/设置将映射到虚拟目录的物理路径。

private string _strPhysicalPath;
       
public string strPhysicalPath
{
    get
    {
        return _strPhysicalPath;
    }
    set
    {
        _strPhysicalPath = value;
    }
}

属性 strVDirName 获取/设置要创建的虚拟目录的名称。

private string _strVDirName;
        
public string strVDirName
{
    get
    {
        return _strVDirName;
    }
    set
    {
        _strVDirName = value;
    }
}

属性 strServerName 获取/设置要在其中创建虚拟目录的服务器的名称。

private string _strServerName;

public string strServerName
{
    get
    {
        return _strServerName;
    }
    set
    {
        _strServerName = value;
    }

}

函数 CreateVDir() 创建虚拟目录并返回成功字符串,或者在出现异常的情况下返回错误消息。

public string CreateVDir()
{
    System.DirectoryServices.DirectoryEntry oDE;
    System.DirectoryServices.DirectoryEntries oDC;
    System.DirectoryServices.DirectoryEntry oVirDir;
    try
    {
        //check whether to create FTP or Web IIS Virtual Directory
        if (this._strDirectoryType == VDirType.WEB_IIS_DIR)
        {
            oDE = new DirectoryEntry("IIS://" + 
                  this._strServerName + "/W3SVC/1/Root");
        }
        else
        {
            oDE = new DirectoryEntry("IIS://" + 
                  this._strServerName + "/MSFTPSVC/1/Root");
        }
        
        //Get Default Web Site
        oDC = oDE.Children;

        //Add row
        oVirDir = oDC.Add(this._strVDirName, 
                  oDE.SchemaClassName.ToString());
        
        //Commit changes for Schema class File
        oVirDir.CommitChanges();

        //Create physical path if it does not exists
        if (!Directory.Exists(this._strPhysicalPath))
        {
            Directory.CreateDirectory(this._strPhysicalPath);
        }

        //Set virtual directory to physical path
        oVirDir.Properties["Path"].Value = this._strPhysicalPath;

        //Set read access
        oVirDir.Properties["AccessRead"][0] = true;

        //Create Application for IIS Application (as for ASP.NET)
        if (this._strDirectoryType == VDirType.WEB_IIS_DIR)
        {
            oVirDir.Invoke("AppCreate", true);
            oVirDir.Properties["AppFriendlyName"][0] = this._strVDirName;
        }
        
        //Save all the changes
        oVirDir.CommitChanges();

        return "Virtual Directory created sucessfully";

    }
    catch (Exception exc)
    {
        return exc.Message.ToString();
    }
}

函数 DeleteVDir() 删除虚拟目录并返回成功字符串,或者在出现异常的情况下返回错误消息。

public string DeleteVDir()
{
    System.DirectoryServices.DirectoryEntry oDE;
    System.DirectoryServices.DirectoryEntries oDC;
    try
    {
        //check whether to delete FTP or Web IIS Virtual Directory
        if (this._strDirectoryType == VDirType.WEB_IIS_DIR)
        {
            oDE = new DirectoryEntry("IIS://" + 
                  this._strServerName + "/W3SVC/1/Root");
        }
        else
        {
            oDE = new DirectoryEntry("IIS://" + 
                      this._strServerName + "/MSFTPSVC/1/Root");
        }
        oDC = oDE.Children;
        
        //Find and remove the row from Directory entry.
        oDC.Remove(oDC.Find(this._strVDirName, 
             oDE.SchemaClassName.ToString()));
        
        //Save the changes
        oDE.CommitChanges();

        return "Virtual Directory deleted sucessfully";

    }
    catch (Exception exc)
    {
        return exc.Message.ToString();
    }
}

为了创建虚拟目录,请将四个属性 strDirectoryTypestrPhysicalPathstrVDirNamestrServerName 设置为您的服务器的名称,并调用函数 CreateVDir()。 同样,您可以在设置了 VdirLib 类的这些属性后,通过调用函数 DeleteVDir() 来删除现有的虚拟目录。

结论

本文介绍如何在 Internet 信息服务器中创建 FTP 或 Web IIS 虚拟目录。

© . All rights reserved.