FTP服务器源码.docx

上传人:b****7 文档编号:9438616 上传时间:2023-02-04 格式:DOCX 页数:15 大小:16.84KB
下载 相关 举报
FTP服务器源码.docx_第1页
第1页 / 共15页
FTP服务器源码.docx_第2页
第2页 / 共15页
FTP服务器源码.docx_第3页
第3页 / 共15页
FTP服务器源码.docx_第4页
第4页 / 共15页
FTP服务器源码.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

FTP服务器源码.docx

《FTP服务器源码.docx》由会员分享,可在线阅读,更多相关《FTP服务器源码.docx(15页珍藏版)》请在冰豆网上搜索。

FTP服务器源码.docx

FTP服务器源码

usingSystem;

usingSystem.IO;

usingSystem.Data;

usingSystem.Collections;

usingLumiSoft.Net.FTP.Server;

namespaceLumiSoft.Net.FTP.Server

{

///

///Virtualftpserver.

///

internalclassls_FTP_Server

{

privateFTP_Serverm_pServer=null;

privatestringm_FtpRoot="";

///

///Defaultconstructor.

///

///Ftprootdirforthisserver.

///IPaddressonwhichtolisten.

///Portonwhichtolisten.

publicls_FTP_Server(stringftpRootDir,stringip,intport)

{

m_FtpRoot=ftpRootDir;

m_pServer=newLumiSoft.Net.FTP.Server.FTP_Server();

this.m_pServer.CommandIdleTimeOut=60000;

this.m_pServer.LogCommands=false;

this.m_pServer.MaxBadCommands=30;

this.m_pServer.SessionIdleTimeOut=800000;

this.m_pServer.Port=port;

this.m_pServer.IpAddress=ip;

this.m_pServer.CreateDir+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnCreateDir);

this.m_pServer.StoreFile+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnStoreFile);

this.m_pServer.DeleteDir+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnDeleteDir);

this.m_pServer.SysError+=newLumiSoft.Net.ErrorEventHandler(this.OnSysError);

this.m_pServer.DirExists+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnDirExists);

this.m_pServer.FileExists+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnFileExists);

this.m_pServer.RenameDirFile+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnRenameDirFile);

this.m_pServer.DeleteFile+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnDeleteFile);

this.m_pServer.ValidateIPAddress+=newLumiSoft.Net.ValidateIPHandler(this.OnValidateIP);

this.m_pServer.GetFile+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnGetFile);

this.m_pServer.GetDirInfo+=newLumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnGetDirInfo);

this.m_pServer.AuthUser+=newLumiSoft.Net.FTP.Server.AuthUserEventHandler(this.OnAuthUser);

m_pServer.Enabled=true;

}

 

#regionEventshandling

#regionFTPserverevents

#regionmethodOnAuthUser

publicvoidOnValidateIP(objectsender,ValidateIP_EventArgse)

{

e.Validated=true;

}

#endregion

#regionmethodOnAuthUser

publicvoidOnAuthUser(objectsender,AuthUser_EventArgse)

{

if(Impersonate.LogonAndImpersonateUser(e.UserName,e.PasswData)){

e.Validated=true;

}

else{

e.Validated=false;

}

}

#endregion

 

#regionmethodOnGetDirInfo

publicvoidOnGetDirInfo(objectsender,FileSysEntry_EventArgse)

{

try{

DataTabledt=e.DirInfo.Tables["DirInfo"];

stringphysicalPath=GetPhysicalPath(e.Name);

//Adddirectories

if(Directory.Exists(physicalPath)){

string[]dirs=Directory.GetDirectories(physicalPath);

foreach(stringdindirs){

DataRowdr=dt.NewRow();

dr["Name"]=newDirectoryInfo(d).Name;

dr["Date"]=Directory.GetCreationTime(d);

//dr["Size"]="";

dr["IsDirectory"]=true;

dt.Rows.Add(dr);

}

//Addvirtualfolders

if(File.Exists(physicalPath+"__Config_ftp.xml")){

try{

DataSetds=newDataSet();

ds.ReadXml(physicalPath+"__Config_ftp.xml");

foreach(DataRowdrinds.Tables["virtualFolder"].Rows){

//ToDo:

ifvirtualfolderissamenameasphysicalfolder

//Addvitualfoldertolistonlyifitexists

stringvDirPhysicalPath=dr["path"].ToString();

if(Directory.Exists(vDirPhysicalPath)){

DataRowdrX=dt.NewRow();

drX["Name"]=dr["name"].ToString();

drX["Date"]=Directory.GetCreationTime(vDirPhysicalPath);

//drX["Size"]="";

drX["IsDirectory"]=true;

dt.Rows.Add(drX);

}

}

}

catch{

}

}

//Addfiles

string[]files=Directory.GetFiles(physicalPath);

foreach(stringfinfiles){

//Hideconfigfile

if(Path.GetFileName(f).ToLower()!

="__config_ftp.xml"){

DataRowdr=dt.NewRow();

dr["Name"]=Path.GetFileName(f);

dr["Date"]=File.GetCreationTime(f);

dr["Size"]=newFileInfo(f).Length;

dr["IsDirectory"]=false;

dt.Rows.Add(dr);

}

}

}

}

catch{

e.Validated=false;

}

}

#endregion

#regionmethodOnDirExists

publicvoidOnDirExists(objectsender,FileSysEntry_EventArgse)

{

if(!

Directory.Exists(GetPhysicalPath(e.Name))){

e.Validated=false;

}

}

#endregion

#regionmethodOnCreateDir

publicvoidOnCreateDir(objectsender,FileSysEntry_EventArgse)

{

if(Directory.Exists(GetPhysicalPath(e.Name))){

e.Validated=false;

}

else{

Directory.CreateDirectory(GetPhysicalPath(e.Name));

}

}

#endregion

#regionmethodOnDeleteDir

publicvoidOnDeleteDir(objectsender,FileSysEntry_EventArgse)

{

if(!

IsVirtualDir(e.Name)&&Directory.Exists(GetPhysicalPath(e.Name))){

Directory.Delete(GetPhysicalPath(e.Name));

}

else{

e.Validated=false;

}

}

#endregion

#regionmethodOnRenameDirFile

publicvoidOnRenameDirFile(objectsender,FileSysEntry_EventArgse)

{

//Removelast/

stringto=e.NewName.Substring(0,e.NewName.Length-1);

stringfrom=e.Name.Substring(0,e.Name.Length-1);

if(IsVirtualDir(to)||IsVirtualDir(from)||Directory.Exists(GetPhysicalPath(to))||File.Exists(GetPhysicalPath(to))){

e.Validated=false;

}

else{

if(Directory.Exists(GetPhysicalPath(from))){

Directory.Move(GetPhysicalPath(from),GetPhysicalPath(to));

}

elseif(File.Exists(GetPhysicalPath(from))){

File.Move(GetPhysicalPath(from),GetPhysicalPath(to));

}

}

}

#endregion

 

#regionmethodOnFileExists

publicvoidOnFileExists(objectsender,FileSysEntry_EventArgse)

{

try{

if(File.Exists(GetPhysicalPath(e.Name))){

e.Validated=true;

}

}

catch{

e.Validated=false;

}

}

#endregion

#regionmethodOnGetFile

publicvoidOnGetFile(objectsender,FileSysEntry_EventArgse)

{

try{

if(File.Exists(GetPhysicalPath(e.Name))){

e.FileStream=File.OpenRead(GetPhysicalPath(e.Name));

}

}

catch{

e.Validated=false;

}

}

#endregion

#regionmethodOnStoreFile

publicvoidOnStoreFile(objectsender,FileSysEntry_EventArgse)

{

try{

if(!

File.Exists(GetPhysicalPath(e.Name))){

e.FileStream=File.Create(GetPhysicalPath(e.Name));

}

}

catch{

e.Validated=false;

}

}

#endregion

#regionmethodOnDeleteFile

publicvoidOnDeleteFile(objectsender,FileSysEntry_EventArgse)

{

if(File.Exists(GetPhysicalPath(e.Name))){

File.Delete(GetPhysicalPath(e.Name));

}

else{

e.Validated=false;

}

}

#endregion

 

#regionmethodOnSysError

publicvoidOnSysError(objectsender,Error_EventArgse)

{

Error.DumpError(e.Exception,e.StackTrace);

}

#endregion

#endregion

#endregion

 

#regionmethodGetPhysicalPath

///

///Getsphysicalpathfromabsolutepath(replacesvirtualfolderswithrealpath).

///

///

///

privatestringGetPhysicalPath(stringpath)

{

stringpPath=m_FtpRoot+"\\";

boolendsWithSep=false;

if(path.StartsWith("/")){

path=path.Substring(1,path.Length-1);

}

if(path.EndsWith("/")){

path=path.Substring(0,path.Length-1);

endsWithSep=true;

}

string[]pathParts=path.Split('/','\\');

foreach(stringpartinpathParts){

if(part.Length>0){

//Thisisphysicaldirectory

if(Directory.Exists(pPath+part)){

pPath+=part+"/";

}

else{

//Seeifvirtualfolder

if(File.Exists(pPath+"__Config_ftp.xml")){

try{

DataSetds=newDataSet();

ds.ReadXml(pPath+"__Config_ftp.xml");

ds.Tables["virtualFolder"].DefaultView.RowFilter="name='"+part+"'";

if(ds.Tables["virtualFolder"].DefaultView.Count>0){

pPath=ds.Tables["virtualFolder"].DefaultView[0]["path"].ToString();

if(!

pPath.EndsWith("\\")){

pPath+="/";

}

}

else{

pPath+=part+"/";

}

}

catch{

}

}

else{

pPath+=part+"/";

}

}

}

}

if(!

endsWithSep&&pPath.EndsWith("/")){

pPath=pPath.Substring(0,pPath.Length-1);

}

returnpPath;

}

#endregion

#regionmethodIsVirtualDir

///

///Getsifspecifieddirisvirtualdirectory.

///

///

///

privateboolIsVirtualDir(stringdir)

{

if(dir.StartsWith("/")){

dir=dir.Substring(1,dir.Length-1);

}

if(dir.EndsWith("/")){

dir=dir.Substring(0,dir.Length-1);

}

stringpPath="";

//---Movedirupandgetphysicalpath

string[]pathParts=dir.Split('/','\\');

if(pathParts.Length>1){

//pPath="";

for(inti=0;i<(pathParts.Length-1);i++){

pPath+=pathParts[i]+"/";

}

if(pPath.Length==0){

pPath="/";

}

}

//else{

//pPath="";

//}

pPath=GetPhysicalPath(pPath);

//---------------------------------------------

if(!

Directory.Exists(pPath+pathParts[pathParts.Length-1])){

//Seeifvirtualfolder

if(File.Exists(pPath+"__Config_ftp.xml")){

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 国外设计风格

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1