需要引用 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[2];
obj[0] = strSchema;
obj[1] = 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:"