日韩欧美人妻无码精品白浆,www.大香蕉久久网,狠狠的日狠狠的操,日本好好热在线观看

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#連接FTP實現(xiàn)文件上傳下載

admin
2024年12月25日 21:41 本文熱度 1227

一、引用DLL


using System.IO;using System.Net;

二、創(chuàng)建FTP連接


/// <summary>  /// 連接FTP服務(wù)器/// </summary>  /// <param name="FtpServerIP">FTP連接地址</param>  /// <param name="FtpRemotePath">指定FTP連接成功后的當(dāng)前目錄, 如果不指定默認(rèn)根目錄</param>  /// <param name="FtpUserID">用戶名</param>  /// <param name="FtpPassword">密碼</param>  public FTPHelper(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword){	System.Net.ServicePointManager.DefaultConnectionLimit = 200;
FtpServerIP = FtpServerIP.Replace("ftp://""");
string[] arr = FtpServerIP.Split('/'); if (arr.Length > 1) { FtpServerIP = arr[0]; for (int i = 1; i < arr.Length; i++) { FtpRemotePath += "/" + arr[i]; } if (FtpRemotePath.Substring(FtpRemotePath.Length - 1) == "/") { FtpRemotePath = FtpRemotePath.Substring(0, FtpRemotePath.Length - 1); } }
ftpServerIP = FtpServerIP; ftpRemotePath = FtpRemotePath; ftpUserID = FtpUserID; ftpPassword = FtpPassword; if (string.IsNullOrEmpty(ftpRemotePath)) { ftpURI = "ftp://" + ftpServerIP + "/"; } else { ftpURI = "ftp://" + ftpServerIP + ftpRemotePath + "/"; } FtpCheckDirectoryExist(ftpRemotePath + "/");}

三、上傳、下載


/// <summary>  /// 上傳  /// </summary>   public void Upload(string filename){	FileInfo fileInf = new FileInfo(filename);	FtpWebRequest reqFTP;	reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileInf.Name));	reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);	reqFTP.Method = WebRequestMethods.Ftp.UploadFile;	reqFTP.UsePassive = false;	reqFTP.KeepAlive = true;	reqFTP.UseBinary = true;	reqFTP.ContentLength = fileInf.Length;	int buffLength = 2048;	byte[] buff = new byte[buffLength];	int contentLen;	FileStream fs = fileInf.OpenRead();	try	{		Stream strm = reqFTP.GetRequestStream();		contentLen = fs.Read(buff, 0, buffLength);		while (contentLen != 0)		{			strm.Write(buff, 0, contentLen);			contentLen = fs.Read(buff, 0, buffLength);		}		strm.Close();		fs.Close();	}	catch (Exception ex)	{		throw new Exception(ex.Message);	}}
/// <summary>  /// 下載  /// </summary>   public void Download(string filePath, string fileName){	try	{		FileInfo fileInf = new FileInfo(fileName);
FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileInf.Name)); reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; reqFTP.UseBinary = true; FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize);
FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); } catch (Exception ex) { throw new Exception(ex.Message); }}

四、創(chuàng)建目錄


/// <summary>/// 判斷文件的目錄是否存,不存則創(chuàng)建 /// </summary>/// <param name="destFilePath"></param>public void FtpCheckDirectoryExist(string destFilePath){	try	{		string fullDir = FtpParseDirectory(destFilePath);		string[] dirs = fullDir.Split('/');		string curDir = "/";		for (int i = 0; i < dirs.Length; i++)		{			string dir = dirs[i];			//如果是以/開始的路徑,第一個為空    			if (dir != null && dir.Length > 0)			{				try				{					curDir += dir + "/";					Uri uri = new Uri("ftp://" + ftpServerIP + curDir);
if (!DirectoryIsExist(uri, ftpUserID, ftpPassword)) { FtpMakeDir(curDir); } } catch (Exception e) { throw new Exception(e.Message); } } } } catch (Exception ex) { throw new Exception("FtpCheckDirectoryExist異常" + ex.Message); }
}
/// <summary>/// 截圖字符/// </summary>/// <param name="destFilePath"></param>/// <returns></returns>public string FtpParseDirectory(string destFilePath){ return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));}
/// <summary>/// 創(chuàng)建目錄/// </summary>/// <param name="localFile"></param>/// <returns></returns>  public Boolean FtpMakeDir(string localFile){ FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + ftpServerIP + localFile); req.Credentials = new NetworkCredential(ftpUserID, ftpPassword); req.Method = WebRequestMethods.Ftp.MakeDirectory; try { FtpWebResponse response = req.GetResponse() as FtpWebResponse; response.Close(); } catch (Exception) { req.Abort(); return false; } req.Abort(); return true;}
//文件是否存在public bool DirectoryIsExist(Uri pFtpServerIP, string pFtpUserID, string pFtpPW){ string[] value = GetFileList(pFtpServerIP, pFtpUserID, pFtpPW); if (value == null) { return false; } else { return true; }}
//獲取文件集public string[] GetFileList(Uri pFtpServerIP, string pFtpUserID, string pFtpPW){ StringBuilder result = new StringBuilder(); try { FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(pFtpServerIP); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(pFtpUserID, pFtpPW); reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append("\n"); line = reader.ReadLine(); } reader.Close(); response.Close(); return result.ToString().Split('\n'); } catch(Exception e) { return null; }}

五、刪除文件


/// <summary>  /// 刪除文件  /// </summary>  public void Delete(string fileName){	try	{		FtpWebRequest reqFTP;		reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));		reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);		reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;		reqFTP.KeepAlive = false;		string result = String.Empty;		FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();		long size = response.ContentLength;		Stream datastream = response.GetResponseStream();		StreamReader sr = new StreamReader(datastream);		result = sr.ReadToEnd();		sr.Close();		datastream.Close();		response.Close();	}	catch (Exception ex)	{		throw new Exception(ex.Message);	}}


該文章在 2024/12/26 9:18:10 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

亚洲国产毛片新片| 天天干天天色天天干天天色天天| 国产超碰精品在线播放| 国产超清一区白虎不卡| 插深深av网站| 亚洲夫妻3p自拍视频| 自拍偷拍国内极品| 天天干天天射特色欧美视频| 久久精品免费看毛片| 亚洲3区久久| 欧美的国产性生活| 五月天婷婷综合影院| 好吊视频日本| 午夜精品国产不卡| 国产精品啪啪一区二区三区| 欧超级欧洲性AV双插| 国内成人综合在线| 国产精品国产一区二区| 天天干天天玩天天射| 91美女干| 亚州无码精品久久久| 女人高潮抽搐喷液10分钟视频| 国产91精品久久久久久第1集| 黑丝高跟一区二区精品视频在线观看| 男的和女的做爽爽爽网站| 欧美另类亚洲| 中文精品中文字母精品视频| 人妻中出字幕一区在线| 美女丁香婷婷| 999国内精品永久免费国内视频| 51成人精品午夜福利| 天天干天天摸天天色天天添天天澡| 蜜臀久久99精品9999| 少妇私密后入网| 亚洲永久精品一| 国产日本精品一区二区免费| A无码一区二区| 日韩国产欧美一区二区在线| 99久久一区二区三区四区| 久久精品国产乱码一区二区三区| 日韩av片中文字幕|