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 = (':') > 0 ? ((':') + 1) : destFilePath; fullDir = ('\\', '/'); string[] dirs = ('/');//Parse out all file names on the path string curDir = "/"; for (int i = 0; i < ; 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 && > 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 ( > 0) { foreach (string str in dirList) { if (() == ()) { return true; } } } return false; }
public string[] GetDirectoryList(string dirName) { string[] drectory = GetFilesDetailList(dirName); List<string> strList = new List<string>(); if ( > 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("<DIR>")) { ((39).Trim()); } else { if (().Substring(0, 1).ToUpper() == "D") { ((55).Trim()); } } } } return (); }
Define ftp address: private string ftpServerIP = "IP:port";
/// <summary> /// Obtain clear documentation /// </summary> /// <param name="path"></param> /// <returns></returns> 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); }
/// <summary> /// Create a directory /// </summary> /// <param name="dirName"></param> 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!