ftp缓存信息

本文详细介绍了如何实现和管理FTP服务,包括获取共享FTP信息、客户端FTP连接、创建目录等功能。通过代码示例展示了从不同角色获取FTP资源的过程,并提供了一种缓存机制来提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System.Collections.Generic;
using NewTempo.Ftp;
using System.IO;
using NshowAdClient.NshowAdServices;
using NshowAdClient.Helper;

namespace NshowAdClient.Services
{
    public static class
        FtpHelpService
    {
        static readonly Dictionary<int, FtpInfo> ClientFtpCache = new Dictionary<int, FtpInfo>();

        static FtpInfo ShareFtpInfo { get; set; }

        static readonly object GetShareFtpLock = new object();
        public static MyFtp GetShareFtp()
        {
            lock (GetShareFtpLock)
            {
                if (LoginUserHelper.Mode == RoleType.Client)
                {
                    return GetClientFtp(LoginUserHelper.UserId);
                }

                if (ShareFtpInfo == null)
                {
                    using (var service = new NshowAdServiceClient())
                    {
                        ShareFtpInfo = service.GetFtpInfo();
                    }
                }

                MyFtp ftp = new MyFtp(ShareFtpInfo.FtpAddress, ShareFtpInfo.User, ShareFtpInfo.Password);
                return ftp;
                //return new MyFtp("61.145.119.85:21", "wcf", "wcf");
            }
        }

        static readonly object GetClientFtpLock = new object();
        public static MyFtp GetClientFtp(int clientId)
        {
            lock (GetClientFtpLock)
            {
                FtpInfo ftpInfo;
                if (ClientFtpCache.ContainsKey(clientId))
                {
                    ftpInfo = ClientFtpCache[clientId];
                }
                else
                {
                    using (var service = new NshowAdServiceClient())
                    {
                        ftpInfo = service.GetClientFtpInfo(clientId);
                        ClientFtpCache.Add(clientId, ftpInfo);
                    }
                }

                var ftp = new MyFtp(ftpInfo.FtpAddress, ftpInfo.User, ftpInfo.Password);
                return ftp;
                //return new MyFtp("61.145.119.85:21", "wcf", "wcf");
            }
        }

        public static MyFtp GetFtp(string ftpIp, string userName, string password)
        {
            return new MyFtp(ftpIp, userName, password);
        }

        public static void CreateDir(string path, MyFtp myFtp)
        {
            var dirName = Path.GetDirectoryName(path);
            bool ret = myFtp.CreateDirectory(dirName);
        }

        public static FtpInfo GetClientFtpInfo(int clientId)
        {
            lock (GetClientFtpLock)
            {
                if (ClientFtpCache.ContainsKey(clientId))
                {
                    return ClientFtpCache[clientId];
                }
                else
                {
                    using (var service = new NshowAdServiceClient())
                    {
                        FtpInfo ftpInfo = service.GetClientFtpInfo(clientId);
                        ClientFtpCache.Add(clientId, ftpInfo);
                        return ftpInfo;
                    }
                }
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/wywnet/p/4755215.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值