1、portPort on which to listen. public ls_FTP_Server(string ftpRootDir,string ip,int port) m_FtpRoot = ftpRootDir; m_pServer = new LumiSoft.Net.FTP.Server.FTP_Server(); this.m_pServer.CommandIdleTimeOut = 60000; this.m_pServer.LogCommands = false; this.m_pServer.MaxBadCommands = 30; this.m_pServer.Sess
2、ionIdleTimeOut = 800000; this.m_pServer.Port = port; this.m_pServer.IpAddress = ip; this.m_pServer.CreateDir += new LumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnCreateDir); this.m_pServer.StoreFile += new LumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnStoreFile); this.m_pServer.D
3、eleteDir += new LumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnDeleteDir); this.m_pServer.SysError += new LumiSoft.Net.ErrorEventHandler(this.OnSysError); this.m_pServer.DirExists += new LumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnDirExists); this.m_pServer.FileExists += new Lum
4、iSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnFileExists); this.m_pServer.RenameDirFile += new LumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnRenameDirFile); this.m_pServer.DeleteFile += new LumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnDeleteFile); this.m_pServer.ValidateI
5、PAddress += new LumiSoft.Net.ValidateIPHandler(this.OnValidateIP); this.m_pServer.GetFile += new LumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnGetFile); this.m_pServer.GetDirInfo += new LumiSoft.Net.FTP.Server.FileSysEntryEventHandler(this.OnGetDirInfo); this.m_pServer.AuthUser += new Lumi
6、Soft.Net.FTP.Server.AuthUserEventHandler(this.OnAuthUser); m_pServer.Enabled = true; #region Events handling #region FTP server events #region method OnAuthUser public void OnValidateIP(object sender,ValidateIP_EventArgs e) e.Validated = true; #endregion public void OnAuthUser(object sender,AuthUser
7、_EventArgs e) if(Impersonate.LogonAndImpersonateUser(e.UserName,e.PasswData) e.Validated = true; else e.Validated = false; #region method OnGetDirInfo public void OnGetDirInfo(object sender,FileSysEntry_EventArgs e) try DataTable dt = e.DirInfo.TablesDirInfo; string physicalPath = GetPhysicalPath(e.
8、Name); / Add directories if(Directory.Exists(physicalPath) string dirs = Directory.GetDirectories(physicalPath); foreach(string d in dirs) DataRow dr = dt.NewRow(); drName = new DirectoryInfo(d).Name;Date = Directory.GetCreationTime(d); / drSize = IsDirectory = true; dt.Rows.Add(dr); / Add virtual f
9、olders if(File.Exists(physicalPath + _Config_ftp.xml) try DataSet ds = new DataSet(); ds.ReadXml(physicalPath + ); foreach(DataRow dr in ds.TablesvirtualFolder.Rows) / ToDo: if virtual folder is same name as physical folder / Add vitual folder to list only if it exists string vDirPhysicalPath = drpa
10、th.ToString(); if(Directory.Exists(vDirPhysicalPath) DataRow drX = dt.NewRow(); drX = drname = Directory.GetCreationTime(vDirPhysicalPath); / drX dt.Rows.Add(drX); catch / Add files string files = Directory.GetFiles(physicalPath); foreach(string f in files) / Hide config file if(Path.GetFileName(f).
11、ToLower() != _config_ftp.xml) DataRow dr = dt.NewRow(); dr = Path.GetFileName(f); = File.GetCreationTime(f); = new FileInfo(f).Length; = false; dt.Rows.Add(dr); catch #region method OnDirExists public void OnDirExists(object sender,FileSysEntry_EventArgs e) if(!Directory.Exists(GetPhysicalPath(e.Nam
12、e) #region method OnCreateDir public void OnCreateDir(object sender,FileSysEntry_EventArgs e) if(Directory.Exists(GetPhysicalPath(e.Name) Directory.CreateDirectory(GetPhysicalPath(e.Name); #region method OnDeleteDir public void OnDeleteDir(object sender,FileSysEntry_EventArgs e) IsVirtualDir(e.Name)
13、 & Directory.Exists(GetPhysicalPath(e.Name) Directory.Delete(GetPhysicalPath(e.Name); else #region method OnRenameDirFile public void OnRenameDirFile(object sender,FileSysEntry_EventArgs e) / Remove last / string to = e.NewName.Substring(0,e.NewName.Length - 1); string from = e.Name.Substring(0,e.Na
14、me.Length - 1); if(IsVirtualDir(to) | IsVirtualDir(from) | Directory.Exists(GetPhysicalPath(to) | File.Exists(GetPhysicalPath(to) if(Directory.Exists(GetPhysicalPath(from) Directory.Move(GetPhysicalPath(from),GetPhysicalPath(to); else if(File.Exists(GetPhysicalPath(from) File.Move(GetPhysicalPath(fr
15、om),GetPhysicalPath(to); #region method OnFileExists public void OnFileExists(object sender,FileSysEntry_EventArgs e) try if(File.Exists(GetPhysicalPath(e.Name) e.Validated = true; #region method OnGetFile public void OnGetFile(object sender,FileSysEntry_EventArgs e) e.FileStream = File.OpenRead(Get
16、PhysicalPath(e.Name); #region method OnStoreFile public void OnStoreFile(object sender,FileSysEntry_EventArgs e) if(!File.Exists(GetPhysicalPath(e.Name) e.FileStream = File.Create(GetPhysicalPath(e.Name); #region method OnDeleteFile public void OnDeleteFile(object sender,FileSysEntry_EventArgs e) if
17、(File.Exists(GetPhysicalPath(e.Name) File.Delete(GetPhysicalPath(e.Name); #region method OnSysError public void OnSysError(object sender,Error_EventArgs e) Error.DumpError(e.Exception,e.StackTrace); #region method GetPhysicalPath / Gets physical path from absolute path (replaces virtual folders with
18、 real path)./returns private string GetPhysicalPath(string path) string pPath = m_FtpRoot + bool endsWithSep = 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(/, foreac
19、h(string part in pathParts) if(part.Length 0) / This is physical directory if(Directory.Exists(pPath + part) pPath += part + else / See if virtual folder if(File.Exists(pPath + ) try DataSet ds = new DataSet(); ds.ReadXml(pPath + ds.Tables.DefaultView.RowFilter = name= + part + if(ds.Tables.DefaultV
20、iew.Count 0) pPath = ds.Tables.DefaultView0 if(!pPath.EndsWith( pPath += else pPath += part + catch else pPath += part + endsWithSep & pPath.EndsWith( pPath = pPath.Substring(0,pPath.Length - 1); return pPath; #region method IsVirtualDir / Gets if specified dir is virtual directory.dir private bool
21、IsVirtualDir(string dir) if(dir.StartsWith( dir = dir.Substring(1,dir.Length - 1); if(dir.EndsWith( dir = dir.Substring(0,dir.Length - 1); string pPath = /- Move dir up and get physical path string pathParts = dir.Split( if(pathParts.Length 1) / pPath = for(int i=0;i(pathParts.Length - 1);i+) pPath += pathPartsi + if(pPath.Length = 0) pPath = / else / pPath = GetPhysicalPath(pPath); /-Directory.Exists(pPath + pathPartspathParts.Length - 1) / See if virtual folder if(File.Exists(pPath +
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1