SoFunction
Updated on 2025-03-06

Implementation of ftp detecting whether the directory exists and creating folders in C#

C# ftp determines whether the directory exists. If it does not exist, it will automatically create a folder.

        /// <summary>
        /// Determine whether the file directory is stored. If it does not exist, create it        /// </summary>
        /// <param name="destFilePath">Local file directory</param>        public void CheckDirectoryAndMakeMyWilson3(string destFilePath)
        {
            string fullDir = (':') &gt; 0 ? ((':') + 1) : destFilePath;
            fullDir = ('\\', '/');
            string[] dirs = ('/');//Parse out all file names on the path            string curDir = "/";
            for (int i = 0; i &lt; ; i++)//Cycle query of each folder            {
                if (dirs[i] == "") continue;
                string dir = dirs[i];
                //If the path starts with /, the first one is empty                if (dir != null &amp;&amp;  &gt; 0)
                {
                    try
                    {
                        CheckDirectoryAndMakeMyWilson2(curDir, dir);
                        curDir += dir + "/";
                    }
                    catch (Exception)
                    { }
                }
            }
        }
        public void CheckDirectoryAndMakeMyWilson2(string rootDir, string remoteDirName)
        {
            if (!DirectoryExist(rootDir, remoteDirName))//Judge whether the subdirectory exists in the current directory                MakeDir(rootDir + "\\" + remoteDirName);
        }
        public bool DirectoryExist(string rootDir, string RemoteDirectoryName)
        {
            string[] dirList = GetDirectoryList(rootDir);//Get subdirectory            if ( &gt; 0)
            {
                foreach (string str in dirList)
                {
                    if (() == ())
                    {
                        return true;
                    }
                }
            }
            return false;
        }
         public string[] GetDirectoryList(string dirName)
        {
            string[] drectory = GetFilesDetailList(dirName);
            List&lt;string&gt; strList = new List&lt;string&gt;();
            if ( &gt; 0)
            {
                foreach (string str in drectory)
                {
                    if (().Length == 0)
                        continue;
                    //There will be returned in two formats.                    //A kind of containing <DIR>                    //A first string is a permission operator symbol like drwxerwxx                    //Now write code to include strings in two formats                    if (().Contains("&lt;DIR&gt;"))
                    {
                        ((39).Trim());
                    }
                    else
                    {
                        if (().Substring(0, 1).ToUpper() == "D")
                        {
                            ((55).Trim());
                        }
                    }
                }
            }
            return ();
        }

Define ftp address: private string ftpServerIP = "IP:port";

         /// &lt;summary&gt;
        /// Obtain clear documentation        /// &lt;/summary&gt;
        /// &lt;param name="path"&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        public string[] GetFilesDetailList(string path)
        {
            return GetFileList("ftp://" + ftpServerIP + "/" + path, );
        }
        //Call this        //The above code examples how to get a file list from an ftp server        private string[] GetFileList(string path, string WRMethods)
        {
            StringBuilder result = new StringBuilder();
            try
            {
                Connect(path);// Establish an FTP connection                 = WRMethods;
                 = false;
                WebResponse response = ();
                StreamReader reader = new StreamReader((), );//Chinese file name                string line = ();
 
                while (line != null)
                {
                    (line);
                    ("\n");
                    line = ();
                }
 
                // to remove the trailing '' '' 
                if (() != "")
                {
                    (().LastIndexOf("\n"), 1);
                }
                ();
                ();
                return ().Split('\n');
            }
 
            catch (Exception ex)
            {
                throw new Exception("Failed to get the file list. Reason: " + );
            }
        }
        //Connect ftp        private void Connect(String path)
        {
            // Create FtpWebRequest object based on uri            reqFTP = (FtpWebRequest)(new Uri(path));
            // Specify the data transfer type             = ;
             = true;
             = false;// means that the connection type is active mode            // ftp username and password             = new NetworkCredential(FTPUSERID, FTPPASSWORD);
        }
         /// &lt;summary&gt;
        /// Create a directory        /// &lt;/summary&gt;
        /// &lt;param name="dirName"&gt;&lt;/param&gt;
        public void MakeDir(string dirName)
        {
            try
            {
                string uri = "ftp://" + ftpServerIP + "/" + dirName;
                Connect(uri);//connect                 = ;
                 = false;
                FtpWebResponse response = (FtpWebResponse)();
                ();
            }
            catch (Exception ex)
            {
                throw new Exception("File creation failed, reason: " + );
            }
        }

This is the article about whether the ftp detection directory exists in C# and the implementation of creating folders. For more related contents of the ftp detection directory, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!