FTP 操作遠端檔案

2021-10-16 17:01:09 字數 4689 閱讀 1785

string ftpuserid;//使用者名稱

string ftppassword;//密碼

ftpwebrequest reqftp;

#region 連線伺服器

/// /// 連線伺服器

///

///

private void connect(string path)

#endregion

#region 獲得檔案列表

/// /// 獲得檔案列表

///

///

///

///

private string getfilelist(string path, string wrmethods)

// to remove the trailing '\n'

result.remove(result.tostring().lastindexof('\n'), 1);

return result.tostring().split('\n');

}catch (exception ex)

finally

if (response != null)}}

/// /// 獲得檔案列表

///

///

///

public string getfilelist(string ftpsubdir)

/// /// 獲得檔案列表

///

///

public string getfilelist()

/// /// 獲得檔案列表明細

///

///

///

public string getfilesdetaillist(string ftpsubdir)

/// /// 獲得檔案列表明細

///

///

public string getfilesdetaillist()

#endregion

/// /// 檔案上傳

///

///

///

///

public bool upload(string filename, string ftpsubdir)

return true;

}catch (exception ex)

finally

if (fs != null)}}

/// /// 檔案上傳

///

///

///

public bool upload(string filename)

///

///

///

///

///

public bool download(string filepath, string filename, string ftpsubdir)

", newfilename));

if (file.exists(newfilename))

string url = "ftp://" + ftpserverip + "/" + ftpsubdir + filename;

log.info(string.format("ftpurl: ", url));

connect(url);//連線

reqftp.credentials = new networkcredential(ftpuserid, ftppassword);

response = (ftpwebresponse)reqftp.getresponse();

ftpstream = response.getresponsestream();

long cl = response.contentlength;

int buffersize = 2048;

int readcount;

byte buffer = new byte[buffersize];

readcount = ftpstream.read(buffer, 0, buffersize);

outputstream = new filestream(newfilename, filemode.create);

while (readcount > 0)

return true;

}catch (exception ex)

finally

if (outputstream != null)

if (response != null)}}

///

///

///

///

public bool download(string filepath, string filename)

#endregion

///

///

///

///

///

public bool download(string filepath, string filename, string ftpsubdir)

", newfilename));

if (file.exists(newfilename))

string url = "ftp://" + ftpserverip + "/" + ftpsubdir + filename;

log.info(string.format("ftpurl: ", url));

connect(url);//連線

reqftp.credentials = new networkcredential(ftpuserid, ftppassword);

response = (ftpwebresponse)reqftp.getresponse();

ftpstream = response.getresponsestream();

long cl = response.contentlength;

int buffersize = 2048;

int readcount;

byte buffer = new byte[buffersize];

readcount = ftpstream.read(buffer, 0, buffersize);

outputstream = new filestream(newfilename, filemode.create);

while (readcount > 0)

return true;

}catch (exception ex)

finally

if (outputstream != null)

if (response != null)}}

/// /// 判斷檔案是否存在

///

///

///

///

public bool fileexists(string filename, string ftpsubdir)

line = reader.readline();

}return success;

}catch (exception ex)

finally

if (response != null)

}}

/// /// 刪除檔案

///

///

///

public bool deletefilename(string filename, string ftpsubdir)

catch (exception ex)

finally

}}

/// /// 建立目錄

///

///

///

///

public bool makedir(string dirname, string ftpsubdir)

catch (exception ex)

finally

}}

/// /// 刪除目錄

///

///

///

public bool deletedir(string dirname)

catch (exception ex)

finally

}}

/// /// 獲得檔案大小

///

///

///

///

public long getfilesize(string filename, string ftpsubdir)

catch (exception ex)

finally

}}

/// /// 檔案改名

///

///

///

///

///

public bool rename(string currentfilename, string newfilename, string ftpsubdir)

catch (exception ex)

finally

}}

FTP遠端傳輸檔案操作

1.與遠端建立連線 ftp ftp open serverip 使用username password登入後即可進行ftp操作。2.目錄操作 ls cd mkdir delete 刪除乙個檔案 mdelete 刪除一批檔案 檔名稱羅列 lcd 跳轉本機目錄 3.檔案傳輸 1 從遠端到本地 ftp g...

FTP檔案操作

using system using system.collections.generic using system.linq using system.text namespace file 獲取伺服器ip public static string serverip 獲取埠 public stat...

ftp讀寫遠端yml檔案

遠端修改prometheus配置的規則檔案 public class prometheusutils ftp.storefile filename,createfileitem filenametemp getinputstream catch ioexception e finally catch...