public virtual bool DeleteFile(FtpContext context, string filename)
        {
            string filepath = GetStoragePath(context, filename);

            if (File.Exists(filepath))
            {
                FileInfo file = new FileInfo(filepath);

                long fileLength = file.Length;

                try
                {
                    File.Delete(filepath);
                    context.ChangeSpace(0 - fileLength);
                    return(true);
                }
                catch (UnauthorizedAccessException uae)
                {
                    AppServer.Logger.Error(uae);
                    context.SetError(FtpCoreResource.PermissionDenied_550);
                    return(false);
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.DeleteFailed_450, filename);
                    return(false);
                }
            }
            else
            {
                context.SetError(FtpCoreResource.NotFound_550);
                return(false);
            }
        }
        protected List <ListItem> GetList(FtpContext context, string dir)
        {
            List <ListItem> list = new List <ListItem>();

            if (Directory.Exists(dir))
            {
                string[] files = Directory.GetFiles(dir);

                if (files != null)
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        FileInfo file = new FileInfo(files[i]);
                        ListItem item = new ListItem();

                        item.ItemType         = ItemType.File;
                        item.LastModifiedTime = file.LastWriteTime;
                        item.Length           = file.Length;
                        item.Name             = file.Name;
                        item.OwnerName        = context.UserName;
                        item.Permission       = "-rwx------";
                        list.Add(item);
                    }
                }

                string[] folders = Directory.GetDirectories(dir);

                if (folders != null)
                {
                    for (int i = 0; i < folders.Length; i++)
                    {
                        DirectoryInfo folder = new DirectoryInfo(folders[i]);
                        ListItem      item   = new ListItem();

                        item.ItemType         = ItemType.Folder;
                        item.LastModifiedTime = folder.LastWriteTime;
                        item.Length           = 0;
                        item.Name             = folder.Name;
                        item.OwnerName        = context.UserName;
                        item.Permission       = "drwx------";

                        list.Add(item);
                    }
                }

                return(list);
            }
            else
            {
                AppServer.Logger.ErrorFormat("{0} was not found.", dir);
                context.SetError(FtpCoreResource.NotFound_550);
                return(null);
            }
        }
        public virtual bool CreateFolder(FtpContext context, string foldername)
        {
            string dir = GetStoragePath(context, foldername);

            if (Directory.Exists(dir) || File.Exists(dir))
            {
                context.SetError(FtpCoreResource.DirectoryAlreadyExist_550, foldername);
                return(false);
            }
            else
            {
                try
                {
                    Directory.CreateDirectory(dir);
                    return(true);
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.MakeDirFailed_550, foldername);
                    return(false);
                }
            }
        }
        public virtual bool IsExistFile(FtpContext context, string filepath)
        {
            string path = GetStoragePath(context, filepath);

            try
            {
                return(File.Exists(path));
            }
            catch (Exception e)
            {
                AppServer.Logger.Error(e);
                context.SetError(FtpCoreResource.FileSystemError_450);
                return(false);
            }
        }
        public virtual DateTime GetModifyTime(FtpContext context, string filename)
        {
            string filepath = GetStoragePath(context, filename);

            if (File.Exists(filepath))
            {
                try
                {
                    FileInfo file = new FileInfo(filepath);
                    return(file.LastWriteTime);
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.FileUnavailable_550);
                    return(DateTime.MinValue);
                }
            }
            else
            {
                context.SetError(FtpCoreResource.NotFound_550);
                return(DateTime.MinValue);
            }
        }
        public virtual long GetFileSize(FtpContext context, string filename)
        {
            string filepath = GetStoragePath(context, filename);

            if (File.Exists(filepath))
            {
                try
                {
                    FileInfo file = new FileInfo(filepath);
                    return(file.Length);
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.FileUnavailable_550);
                    return(0);
                }
            }
            else
            {
                context.SetError(FtpCoreResource.NotFound_550);
                return(0);
            }
        }
        public virtual bool RemoveFolder(FtpContext context, string foldername)
        {
            string dir = GetStoragePath(context, foldername);

            if (Directory.Exists(dir))
            {
                try
                {
                    Directory.Delete(dir);
                    return(true);
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.RemoveDirectoryFailed_550, foldername);
                    return(false);
                }
            }
            else
            {
                context.SetError(FtpCoreResource.NotFound_550);
                return(false);
            }
        }
        public virtual bool RenameFolder(FtpContext context, string oldPath, string newPath)
        {
            oldPath = GetStoragePath(context, oldPath);
            newPath = GetStoragePath(context, newPath);

            try
            {
                Directory.Move(oldPath, newPath);
                return(true);
            }
            catch (Exception e)
            {
                AppServer.Logger.Error(e);
                context.SetError(FtpCoreResource.RenameDirectoryFailed_553);
                return(false);
            }
        }
        public virtual bool IsExistFolder(FtpContext context, string path)
        {
            string dir = GetStoragePath(context, path);

            try
            {
                bool result = Directory.Exists(dir);

                if (!result)
                {
                    AppServer.Logger.Error("The directory: " + dir + " was not found");
                }

                return(result);
            }
            catch (Exception e)
            {
                AppServer.Logger.Error(e);
                context.SetError(FtpCoreResource.FileSystemError_450);
                return(false);
            }
        }
        public virtual bool ReadFile(FtpContext context, string filename, Stream stream)
        {
            int bufLen = 1024 * 10;
            byte[] buffer = new byte[bufLen];
            int read = 0;

            FileStream fs = null;

            try
            {
                string filePath = GetStoragePath(context, Path.Combine(context.CurrentPath, filename));

                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufLen);

                if (context.Offset > 0)
                    fs.Seek(context.Offset, SeekOrigin.Begin);

                DateTime dtStart;
                TimeSpan ts;
                int usedMs = 0, predictMs = 0;
                int speed = context.User.MaxDownloadSpeed;

                dtStart = DateTime.Now;

                while ((read = fs.Read(buffer, 0, bufLen)) > 0)
                {
                    stream.Write(buffer, 0, read);

                    if (speed > 0) // if speed <=0, then no speed limitation
                    {
                        ts = DateTime.Now.Subtract(dtStart);
                        usedMs = (int)ts.TotalMilliseconds;
                        predictMs = read / speed;

                        if (predictMs > usedMs) //Speed control
                            Thread.Sleep(predictMs - usedMs);

                        dtStart = DateTime.Now;
                    }
                }

                return true;
            }
            catch (Exception e)
            {
                AppServer.Logger.Error(e);
                context.SetError("Failed to delete file");
                return false;
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                    fs = null;
                }
            }

        }
        public virtual DateTime GetModifyTime(FtpContext context, string filename)
        {
            string filepath = GetStoragePath(context, filename);

            if (File.Exists(filepath))
            {
                try
                {
                    FileInfo file = new FileInfo(filepath);
                    return file.LastWriteTime;
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.FileUnavailable_550);
                    return DateTime.MinValue;
                }
            }
            else
            {
                context.SetError(FtpCoreResource.NotFound_550);
                return DateTime.MinValue;
            }
        }
        public virtual bool DeleteFile(FtpContext context, string filename)
        {
            string filepath = GetStoragePath(context, filename);

            if (File.Exists(filepath))
            {
                FileInfo file = new FileInfo(filepath);

                long fileLength = file.Length;

                try
                {
                    File.Delete(filepath);
                    context.ChangeSpace(0 - fileLength);
                    return true;
                }
                catch (UnauthorizedAccessException uae)
                {
                    AppServer.Logger.Error(uae);
                    context.SetError(FtpCoreResource.PermissionDenied_550);
                    return false;
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.DeleteFailed_450, filename);
                    return false;
                }
            }
            else
            {
                context.SetError(FtpCoreResource.NotFound_550);
                return false;
            }
        }
        public virtual bool RemoveFolder(FtpContext context, string foldername)
        {
            string dir = GetStoragePath(context, foldername);

            if (Directory.Exists(dir))
            {
                try
                {
                    Directory.Delete(dir);
                    return true;
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.RemoveDirectoryFailed_550, foldername);
                    return false;
                }
            }
            else
            {
                context.SetError(FtpCoreResource.NotFound_550);
                return false;
            }
        }
        public virtual bool CreateFolder(FtpContext context, string foldername)
        {
            string dir = GetStoragePath(context, foldername);

            if (Directory.Exists(dir) || File.Exists(dir))
            {
                context.SetError(FtpCoreResource.DirectoryAlreadyExist_550, foldername);
                return false;
            }
            else
            {
                try
                {
                    Directory.CreateDirectory(dir);
                    return true;
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.MakeDirFailed_550, foldername);
                    return false;
                }
            }
        }
        public virtual bool IsExistFile(FtpContext context, string filepath)
        {
            string path = GetStoragePath(context, filepath);

            try
            {
                return File.Exists(path);
            }
            catch (Exception e)
            {
                AppServer.Logger.Error(e);
                context.SetError(FtpCoreResource.FileSystemError_450);
                return false;
            }
        }
        public virtual bool IsExistFolder(FtpContext context, string path)
        {
            string dir = GetStoragePath(context, path);

            try
            {
                bool result = Directory.Exists(dir);

                if (!result)
                {
                    AppServer.Logger.Error("The directory: " + dir + " was not found");
                }

                return result;
            }
            catch (Exception e)
            {
                AppServer.Logger.Error(e);
                context.SetError(FtpCoreResource.FileSystemError_450);
                return false;
            }
        }
        public virtual bool RenameFolder(FtpContext context, string oldPath, string newPath)
        {
            oldPath = GetStoragePath(context, oldPath);
            newPath = GetStoragePath(context, newPath);

            try
            {
                Directory.Move(oldPath, newPath);
                return true;
            }
            catch (Exception e)
            {
                AppServer.Logger.Error(e);
                context.SetError(FtpCoreResource.RenameDirectoryFailed_553);
                return false;
            }
        }
        public virtual long GetFileSize(FtpContext context, string filename)
        {
            string filepath = GetStoragePath(context, filename);

            if (File.Exists(filepath))
            {
                try
                {
                    FileInfo file = new FileInfo(filepath);
                    return file.Length;
                }
                catch (Exception e)
                {
                    AppServer.Logger.Error(e);
                    context.SetError(FtpCoreResource.FileUnavailable_550);
                    return 0;
                }
            }
            else
            {
                context.SetError(FtpCoreResource.NotFound_550);
                return 0;
            }
        }
        public virtual bool ReadFile(FtpContext context, string filename, Stream stream)
        {
            int bufLen = 1024 * 10;

            byte[] buffer = new byte[bufLen];
            int    read   = 0;

            FileStream fs = null;

            try
            {
                string filePath = GetStoragePath(context, Path.Combine(context.CurrentPath, filename));

                fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufLen);

                if (context.Offset > 0)
                {
                    fs.Seek(context.Offset, SeekOrigin.Begin);
                }

                DateTime dtStart;
                TimeSpan ts;
                int      usedMs = 0, predictMs = 0;
                int      speed = context.User.MaxDownloadSpeed;

                dtStart = DateTime.Now;

                while ((read = fs.Read(buffer, 0, bufLen)) > 0)
                {
                    stream.Write(buffer, 0, read);

                    if (speed > 0) // if speed <=0, then no speed limitation
                    {
                        ts        = DateTime.Now.Subtract(dtStart);
                        usedMs    = (int)ts.TotalMilliseconds;
                        predictMs = read / speed;

                        if (predictMs > usedMs) //Speed control
                        {
                            Thread.Sleep(predictMs - usedMs);
                        }

                        dtStart = DateTime.Now;
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                AppServer.Logger.Error(e);
                context.SetError("Failed to delete file");
                return(false);
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                    fs.Dispose();
                    fs = null;
                }
            }
        }
        protected List<ListItem> GetList(FtpContext context, string dir)
        {
            List<ListItem> list = new List<ListItem>();

            if (Directory.Exists(dir))
            {
                string[] files = Directory.GetFiles(dir);

                if (files != null)
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        FileInfo file = new FileInfo(files[i]);
                        ListItem item = new ListItem();

                        item.ItemType = ItemType.File;
                        item.LastModifiedTime = file.LastWriteTime;
                        item.Length = file.Length;
                        item.Name = file.Name;
                        item.OwnerName = context.UserName;
                        item.Permission = "-rwx------";
                        list.Add(item);
                    }
                }

                string[] folders = Directory.GetDirectories(dir);

                if (folders != null)
                {
                    for (int i = 0; i < folders.Length; i++)
                    {
                        DirectoryInfo folder = new DirectoryInfo(folders[i]);
                        ListItem item = new ListItem();

                        item.ItemType = ItemType.Folder;
                        item.LastModifiedTime = folder.LastWriteTime;
                        item.Length = 0;
                        item.Name = folder.Name;
                        item.OwnerName = context.UserName;
                        item.Permission = "drwx------";

                        list.Add(item);
                    }
                }

                return list;
            }
            else
            {
                AppServer.Logger.ErrorFormat("{0} was not found.", dir);
                context.SetError(FtpCoreResource.NotFound_550);
                return null;
            }
        }