妖怪幻 发表于 2015-5-29 09:44:03

创建FTP和Web虚拟目录的方法!

需要引用 using System.DirectoryServices.dll;

1. 创建FTP
private strRootSubPath = "/MSFTPSVC/1/Root";
private strSchema = "IIsFtpVirtualDir";
上面两个字符串是系统的
private DirectoryEntry deRoot = new DirectoryEntry("IIS://" + ComputerName | IP |+ strRootSubPath );
// 例 DirectoryEntry deRoot = new DirectoryEntry( "IIS://" + "localhost" + "/MSFTPSVC/1/Root" );

//判断是否存在相同别名的FTP名
private bool ExistFtp( string FtpName )
{
   bool Exist = false;
   try
   {
            DirectoryEntries dir = deRoot.Children;
   
            foreach( DirectoryEntry dd in dir )
            {
                     if ( dd.Name.Equals( FtpName ) )
                     {
                                 Exist = true;
                     }
            }
            return Exist;
   }
   catch
   {
            return false;
   }
}
///删除FTP
private bool DeleteFtp( string FtpName )
{
   try
   {
   //删除FTP站点
    object[] obj = new object;
    obj = strSchema;
    obj = FtpName
    deRoot.Invoke("Delete",obj);
    deRoot.CommitChanges();
    return true;
   }
   catch
   {
    return false;
   }

public bool CreateFtp()
{
      try
      {
                  if ( this.ExistFtp(ftpName) )
                        {
                                     this.DeleteFtp( ftpName);
                        }
                        deRoot.RefreshCache();
                        DirectoryEntry deNewVDir = deRoot.Children.Add(txtVDirName.Text,strSchema);   
   
                         deNewVDir.Properties["Path"].Insert(0,txtVDir.Text);
                        deNewVDir.CommitChanges();
                        deRoot.CommitChanges();
                        deNewVDir.Close();
                        return true;
          }
          catch
      {
                  return false;
      }
      finally
      {
             deRoot.Close();
      }
}


2.给目标机子上加用户
try
   {
    DirectoryEntry AD = new DirectoryEntry("WinNT://" +   "127.0.0.1"+ ",computer");
    DirectoryEntry ds = AD.Children.Find("Administrator","user");
    DirectoryEntry NewUser = AD.Children.Add("llr", "user");
    NewUser.Invoke("SetPassword", new object[] {"llr"});
    NewUser.Invoke("Put", new object[] {"Description", "Test User from .NET"});
    NewUser.CommitChanges();
    DirectoryEntry grp;
    grp = AD.Children.Find("Administrators", "group");
    if (grp != null)
    {
   grp.Invoke("Add", new object[] {NewUser.Path.ToString()});
    }
   
   }
   catch (Exception ex)
   {
    MessageBox.Show(ex.Message);
   }

注:
Ftp属性请找 IIsFtpServerSetting。 Web 属性请找IIsWebServerSetting
特别的 Ftp 端口号设置是    ServerBindings[]    赋值的格式   ":21:"
页: [1]
查看完整版本: 创建FTP和Web虚拟目录的方法!