c# 在windows中操作IIS设置FTP服务器的示例
短信预约 -IT技能 免费直播动态提醒
什么是FTP
FTP(File Transfer Protocol)是TCP/IP网络上两台计算机传送文件的协议,使得主机间可以共享文件.可以将 Internet 信息服务 (IIS) 配置为作为 FTP 服务器来运行。 这样,其他计算机便可以连接到服务器并将文件复制到服务器或者从服务器复制文件。 例如,如果您在自己的计算机上承载网站,并且希望允许远程用户连接到您的计算机并将他们的文件复制到服务器,则可以将 IIS 配置为充当 FTP 服务器。
主要实现方式
下面主要讲解一下,在Window的IIS中创建FTP的Site。
1、创建站点
public int createFtpSite(string ftpname,string path){
int errorCode = ErrorCode.Succeed;
if (ftpname == "" && path == "")
{
try
{
ServerManager iisManager = new ServerManager();
Configuration cfg = iisManager.GetApplicationHostConfiguration();
try
{
foreach (var ftpsite in iisManager.Sites)
{
string sitename = ftpsite.Name;
foreach (Binding binding in ftpsite.Bindings)
{
try
{
string currentServerBindings = binding.GetAttributeValue("BindingInformation").ToString();
string port = currentServerBindings.Split(":".ToArray())[1];
if (port == "21")
{
try
{
//stop site
ftpsite.Stop();
}
catch
{
//doing nothing
}
break;
}
}
catch
{
//doing nothing
}
}
}
//提交更改
iisManager.CommitChanges();
}
catch
{
//do nothing
}
if (!System.IO.Directory.Exists(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath")))//创建站点路径
{
System.IO.Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath"));
}
Site site = iisManager.Sites.Add(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp"), "ftp", string.Format("*:{0}:", "21"), System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath"));
iisManager.CommitChanges();
//设置FTP SSL权限
SetFtpSSL();
//设置FTP Everyone权限
IISUtil.IISCore.AddSiteUtil addsiteUtil = new AddSiteUtil();
try
{
string config_rootpath = System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath");
//string rootpath = path.Substring(0, path.IndexOf(ftpname) - 1) + "\\ftproot";
if (!System.IO.Directory.Exists(config_rootpath))
{
System.IO.Directory.CreateDirectory(config_rootpath);
}
addsiteUtil.icaclsSet("Everyone", System.Configuration.ConfigurationManager.AppSettings.Get("defaultftpath"));
System.IO.File.SetAttributes(config_rootpath, System.IO.FileAttributes.Hidden);
}
catch
{
}
}
catch
{
errorCode = ErrorCode.ftpSiteFail;
}
}
else
{
if (!getFtpState(ftpname))//判断ftp用户是否存在
{
FtpStateInit();
try
{
using (ServerManager iisManager = new ServerManager())
{
Site site = iisManager.Sites.FirstOrDefault(o => ((string)o["name"]).Contains(System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp")));
var vird = site.Applications[0].VirtualDirectories["/" + ftpname];
if (vird == null) { site.Applications[0].VirtualDirectories.Add("/" + ftpname, path); }
else { errorCode = ErrorCode.ftpExists; }
iisManager.CommitChanges();
//添加FTP访问权限
SetFtpAccess(ftpname);
}
}
catch
{
errorCode = ErrorCode.ftpSiteFail;
}
}
else
{
errorCode = ErrorCode.ftpExists;
}
}
return errorCode;
}
2、站点列表
/// <summary>
/// iis6获取所有ftp站点信息
/// </summary>
/// <param name="newsitename"></param>
/// <returns></returns>
public static List<string> iGetFtpInfos()
{
List<string> ftpinfos = new List<string>();
try
{
string ftproot = System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp");
string ftpname = "";//用户名
string ftppass = "";//密码
string ftppath = "";//物理路径
string iisversion = "";//iis版本
string majorversion = IISCore.IISInfoUtil.SGetIISMajorVersion();
if (majorversion == "")
{
iisversion = "未知";
}
else
{
iisversion = majorversion.ToString();
}
var siteEntry = new DirectoryEntry("IIS://localhost/MSFTPSVC");//IIS6管理对象
DirectoryEntry rootentry = new DirectoryEntry("IIS://localhost/W3SVC");//创建IIS管理对象
foreach (DirectoryEntry sitechild in siteEntry.Children)
{
if (!sitechild.SchemaClassName.EqualsEx("IIsFtpServer")) //IIsFtpServer代表FTP
continue;
string yftpname = sitechild.Properties["ServerComment"].Value.ToString();
string defaultftpname = System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp");
if (yftpname == defaultftpname)
{
try
{
//获取站点信息
var root = sitechild.Children.Find("ROOT", "IIsFtpVirtualDir");
DirectoryEntries ftps = root.Children;
foreach (DirectoryEntry ftp in ftps)
{
ftpname = ftp.Name;
try
{
foreach (DirectoryEntry child in rootentry.Children)
{
if (child.SchemaClassName == "IIsWebServer" && child.Properties["ServerComment"].Value.ToString() == ftpname)
{
ftppass = child.Properties["AnonymousUserPass"].Value.ToString();
foreach (DirectoryEntry rootChild in child.Children)
{
string name = rootChild.Name.ToString();
if ((rootChild.SchemaClassName == "IIsWebVirtualDir") && (rootChild.Name.ToString().ToLower() == "root"))
{
if (rootChild.Properties["Path"].Value == null)
{
ftppath = "";
}
else
{
ftppath = rootChild.Properties["Path"].Value.ToString().Substring(0, rootChild.Properties["Path"].Value.ToString().LastIndexOf("\\"));
}
}
}
}
}
}
catch
{
}
if(ftpname != "")
ftpinfos.Add(ftproot + "-@-" + ftpname + "-@-" + ftppass + "-@-" + ftppath + "-@-" + iisversion);//添加到站点信息
}
}
catch
{
}
}
}
}
catch
{
}
return ftpinfos;//返回数据
}
3、删除站点
public static bool DeleteQFtp(string ftpname)
{
bool flag = false;
try{
var siteEntry = new DirectoryEntry("IIS://localhost/MSFTPSVC");//IIS6管理对象
if (ftpname != "")
{
foreach (DirectoryEntry sitechild in siteEntry.Children)
{
if (!sitechild.SchemaClassName.EqualsEx("IIsFtpServer")) //IIsFtpServer代表FTP
continue;
string yftpname = sitechild.Properties["ServerComment"].Value.ToString();
if (yftpname.ToLower() == System.Configuration.ConfigurationManager.AppSettings.Get("defaultftp").ToLower())
{
try
{
DirectoryEntry root = sitechild.Children.Find("ROOT", "IIsFtpVirtualDir");
var ftpchild = root.Children.Find(ftpname, "IIsFtpVirtualDir");
if (ftpchild != null)
{
//删除
root.Children.Remove(ftpchild);
root.CommitChanges();
sitechild.CommitChanges();
siteEntry.CommitChanges();
flag = true;
}
}
catch
{
flag = false;
}
}
}
}
}
catch
{
}
return flag;
}
以上就是c# 在windows中操作IIS设置FTP服务器的示例的详细内容,更多关于c# 设置FTP服务器的资料请关注编程网其它相关文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341