C#中ftp檢測目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn)
C# ftp判斷目錄是否存在,不存在則自動(dòng)創(chuàng)建文件夾
/// <summary>
/// 判斷文件的目錄是否存,不存則創(chuàng)建
/// </summary>
/// <param name="destFilePath">本地文件目錄</param>
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++)//循環(huán)查詢每一個(gè)文件夾
{
if (dirs[i] == "") continue;
string dir = dirs[i];
//如果是以/開始的路徑,第一個(gè)為空
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))//判斷當(dāng)前目錄下子目錄是否存在
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<string> strList = new List<string>();
if (drectory.Length > 0)
{
foreach (string str in drectory)
{
if (str.Trim().Length == 0)
continue;
//會(huì)有兩種格式的詳細(xì)信息返回
//一種包含<DIR>
//一種第一個(gè)字符串是drwxerwxx這樣的權(quán)限操作符號(hào)
//現(xiàn)在寫代碼包容兩種格式的字符串
if (str.Trim().Contains("<DIR>"))
{
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:端口";
/// <summary>
/// 獲得文件明晰
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string[] GetFilesDetailList(string path)
{
return GetFileList("ftp://" + ftpServerIP + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails);
}
//都調(diào)用這個(gè)
//上面的代碼示例了如何從ftp服務(wù)器上獲得文件列表
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)
{
// 根據(jù)uri創(chuàng)建FtpWebRequest對象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// 指定數(shù)據(jù)傳輸類型
reqFTP.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.UsePassive = false;//表示連接類型為主動(dòng)模式
// ftp用戶名和密碼
reqFTP.Credentials = new NetworkCredential(FTPUSERID, FTPPASSWORD);
}
/// <summary>
/// 創(chuàng)建目錄
/// </summary>
/// <param name="dirName"></param>
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("創(chuàng)建文件失敗,原因: " + ex.Message);
}
}
到此這篇關(guān)于C#中ftp檢測目錄是否存在和創(chuàng)建文件夾的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# ftp檢測目錄存在內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#字符串內(nèi)存分配與駐留池學(xué)習(xí)分享
這篇文章主要介紹了C#字符串內(nèi)存分配與駐留池學(xué)習(xí)分享,大家參考使用吧2013-12-12
C#實(shí)現(xiàn)窗體間傳遞數(shù)據(jù)實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)窗體間傳遞數(shù)據(jù)實(shí)例,需要的朋友可以參考下2014-07-07
C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼
這篇文章主要介紹了C# 實(shí)現(xiàn)截圖軟件功能實(shí)例代碼,需要的朋友可以參考下2017-06-06
基于WebClient實(shí)現(xiàn)Http協(xié)議的Post與Get對網(wǎng)站進(jìn)行模擬登陸和瀏覽實(shí)例
這篇文章主要介紹了基于WebClient實(shí)現(xiàn)Http協(xié)議的Post與Get對網(wǎng)站進(jìn)行模擬登陸和瀏覽的方法,以實(shí)例形式詳細(xì)分析了WebClient模擬POST與GET登陸與瀏覽的過程,對于C#項(xiàng)目開發(fā)來說具有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11
C#實(shí)現(xiàn)對用戶輸入數(shù)據(jù)進(jìn)行校驗(yàn)的類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)對用戶輸入數(shù)據(jù)進(jìn)行校驗(yàn)的類,實(shí)例分析了C#針對各種用戶輸入數(shù)據(jù)的常用校驗(yàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
Unity?UGUI的CanvasScaler畫布縮放器組件介紹使用
這篇文章主要為大家介紹了Unity?UGUI的CanvasScaler畫布縮放器組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C# WinForms存儲(chǔ)過程操作數(shù)據(jù)庫的實(shí)例講解
這篇文章主要介紹了C# WinForms存儲(chǔ)過程操作數(shù)據(jù)庫的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04

