135-1821-9792

如何用C#ftp检测目录是否存在和创建文件夹

这篇文章主要介绍了如何用C# ftp检测目录是否存在和创建文件夹的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇如何用C# ftp检测目录是否存在和创建文件夹文章都会有所收获,下面我们一起来看看吧。

为随县等地区用户提供了全套网页设计制作服务,及随县网站建设行业解决方案。主营业务为网站设计、做网站、随县网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

C# ftp判断目录是否存在,不存在则自动创建文件夹

        /// 
        /// 判断文件的目录是否存,不存则创建
        /// 
        /// 本地文件目录
        public void CheckDirectoryAndMakeMyWilson3(string destFilePath)
        {
            string fullDir = destFilePath.IndexOf(':') > 0 ? destFilePath.Substring(destFilePath.IndexOf(':') + 1) : destFilePath;
            fullDir = fullDir.Replace('\\', '/');
            string[] dirs = fullDir.Split('/');//解析出路径上所有的文件名
            string curDir = "/";
            for (int i = 0; i < dirs.Length; i++)//循环查询每一个文件夹
            {
                if (dirs[i] == "") continue;
                string dir = dirs[i];
                //如果是以/开始的路径,第一个为空 
                if (dir != null && dir.Length > 0)
                {
                    try
                    {
                        CheckDirectoryAndMakeMyWilson2(curDir, dir);
                        curDir += dir + "/";
                    }
                    catch (Exception)
                    { }
                }
            }
        }
        public void CheckDirectoryAndMakeMyWilson2(string rootDir, string remoteDirName)
        {
            if (!DirectoryExist(rootDir, remoteDirName))//判断当前目录下子目录是否存在
                MakeDir(rootDir + "\\" + remoteDirName);
        }
        public bool DirectoryExist(string rootDir, string RemoteDirectoryName)
        {
            string[] dirList = GetDirectoryList(rootDir);//获取子目录
            if (dirList.Length > 0)
            {
                foreach (string str in dirList)
                {
                    if (str.Trim() == RemoteDirectoryName.Trim())
                    {
                        return true;
                    }
                }
            }
            return false;
        }
         public string[] GetDirectoryList(string dirName)
        {
            string[] drectory = GetFilesDetailList(dirName);
            List strList = new List();
            if (drectory.Length > 0)
            {
                foreach (string str in drectory)
                {
                    if (str.Trim().Length == 0)
                        continue;
                    //会有两种格式的详细信息返回
                    //一种包含
                    //一种第一个字符串是drwxerwxx这样的权限操作符号
                    //现在写代码包容两种格式的字符串
                    if (str.Trim().Contains(""))
                    {
                        strList.Add(str.Substring(39).Trim());
                    }
                    else
                    {
                        if (str.Trim().Substring(0, 1).ToUpper() == "D")
                        {
                            strList.Add(str.Substring(55).Trim());
                        }
                    }
                }
            }
            return strList.ToArray();
        }

定义ftp地址:private string ftpServerIP = "IP:端口";

         /// 
        /// 获得文件明晰
        /// 
        /// 
        /// 
        public string[] GetFilesDetailList(string path)
        {
            return GetFileList("ftp://" + ftpServerIP + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails);
        }
        //都调用这个
        //上面的代码示例了如何从ftp服务器上获得文件列表
        private string[] GetFileList(string path, string WRMethods)
        {
            StringBuilder result = new StringBuilder();
            try
            {
                Connect(path);//建立FTP连接
                reqFTP.Method = WRMethods;
                reqFTP.KeepAlive = false;
                WebResponse response = reqFTP.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);//中文文件名
                string line = reader.ReadLine();
 
                while (line != null)
                {
                    result.Append(line);
                    result.Append("\n");
                    line = reader.ReadLine();
                }
 
                // to remove the trailing '' '' 
                if (result.ToString() != "")
                {
                    result.Remove(result.ToString().LastIndexOf("\n"), 1);
                }
                reader.Close();
                response.Close();
                return result.ToString().Split('\n');
            }
 
            catch (Exception ex)
            {
                throw new Exception("获取文件列表失败。原因: " + ex.Message);
            }
        }
        //连接ftp
        private void Connect(String path)
        {
            // 根据uri创建FtpWebRequest对象
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
            // 指定数据传输类型
            reqFTP.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.UsePassive = false;//表示连接类型为主动模式
            // ftp用户名和密码
            reqFTP.Credentials = new NetworkCredential(FTPUSERID, FTPPASSWORD);
        }
         /// 
        /// 创建目录
        /// 
        /// 
        public void MakeDir(string dirName)
        {
            try
            {
                string uri = "ftp://" + ftpServerIP + "/" + dirName;
                Connect(uri);//连接       
                reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                reqFTP.KeepAlive = false;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                response.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("创建文件失败,原因: " + ex.Message);
            }
        }

关于“如何用C# ftp检测目录是否存在和创建文件夹”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“如何用C# ftp检测目录是否存在和创建文件夹”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注创新互联行业资讯频道。


标题名称:如何用C#ftp检测目录是否存在和创建文件夹
网站地址:http://kswsj.com/article/popdod.html

其他资讯



Copyright © 2009-2022 www.kswsj.com 成都快上网科技有限公司 版权所有 蜀ICP备19037934号